搜索
首页后端开发php教程一个php把一组文件打包成zip的类

这段php类可以挨个添加文件到数组,最后将添加的文件打包成zip

  1. /* $Id: zip.lib.php,v 1.1 2004/02/14 15:21:18 anoncvs_tusedb Exp $ */
  2. // vim: expandtab sw=4 ts=4 sts=4:
  3. /**
  4. * Zip file creation class.
  5. * Makes zip files.
  6. *
  7. * Last Modification and Extension By :
  8. *
  9. * Hasin Hayder
  10. * HomePage : www.hasinme.info
  11. * Email : countdraculla@gmail.com
  12. * IDE : PHP Designer 2005
  13. *
  14. *
  15. * Originally Based on :
  16. *
  17. * http://www.zend.com/codex.php?id=535&single=1
  18. * By Eric Mueller
  19. *
  20. * http://www.zend.com/codex.php?id=470&single=1
  21. * by Denis125
  22. *
  23. * a patch from Peter Listiak for last modified
  24. * date and time of the compressed file
  25. *
  26. * Official ZIP file format: http://www.pkware.com/appnote.txt
  27. *
  28. * @access public
  29. */
  30. class zipfile
  31. {
  32. /**
  33. * Array to store compressed data
  34. *
  35. * @public array $datasec
  36. */
  37. public $datasec = array();
  38. /**
  39. * Central directory
  40. *
  41. * @public array $ctrl_dir
  42. */
  43. public $ctrl_dir = array();
  44. /**
  45. * End of central directory record
  46. *
  47. * @public string $eof_ctrl_dir
  48. */
  49. public $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  50. /**
  51. * Last offset position
  52. *
  53. * @public integer $old_offset
  54. */
  55. public $old_offset = 0;
  56. /**
  57. * Converts an Unix timestamp to a four byte DOS date and time format (date
  58. * in high two bytes, time in low two bytes allowing magnitude comparison).
  59. *
  60. * @param integer the current Unix timestamp
  61. *
  62. * @return integer the current date in a four byte DOS format
  63. *
  64. * @access private
  65. */
  66. function unix2DosTime($unixtime = 0) {
  67. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  68. if ($timearray['year'] $timearray['year'] = 1980;
  69. $timearray['mon'] = 1;
  70. $timearray['mday'] = 1;
  71. $timearray['hours'] = 0;
  72. $timearray['minutes'] = 0;
  73. $timearray['seconds'] = 0;
  74. } // end if
  75. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);
  76. } // end of the 'unix2DosTime()' method
  77. /**
  78. * Adds "file" to archive
  79. *
  80. * @param string file contents
  81. * @param string name of the file in the archive (may contains the path)
  82. * @param integer the current timestamp
  83. *
  84. * @access public
  85. */
  86. function addFile($data, $name, $time = 0)
  87. {
  88. $name = str_replace('\\', '/', $name);
  89. $dtime = dechex($this->unix2DosTime($time));
  90. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  91. . '\x' . $dtime[4] . $dtime[5]
  92. . '\x' . $dtime[2] . $dtime[3]
  93. . '\x' . $dtime[0] . $dtime[1];
  94. eval('$hexdtime = "' . $hexdtime . '";');
  95. $fr = "\x50\x4b\x03\x04";
  96. $fr .= "\x14\x00"; // ver needed to extract
  97. $fr .= "\x00\x00"; // gen purpose bit flag
  98. $fr .= "\x08\x00"; // compression method
  99. $fr .= $hexdtime; // last mod time and date
  100. // "local file header" segment
  101. $unc_len = strlen($data);
  102. $crc = crc32($data);
  103. $zdata = gzcompress($data);
  104. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  105. $c_len = strlen($zdata);
  106. $fr .= pack('V', $crc); // crc32
  107. $fr .= pack('V', $c_len); // compressed filesize
  108. $fr .= pack('V', $unc_len); // uncompressed filesize
  109. $fr .= pack('v', strlen($name)); // length of filename
  110. $fr .= pack('v', 0); // extra field length
  111. $fr .= $name;
  112. // "file data" segment
  113. $fr .= $zdata;
  114. // "data descriptor" segment (optional but necessary if archive is not
  115. // served as file)
  116. $fr .= pack('V', $crc); // crc32
  117. $fr .= pack('V', $c_len); // compressed filesize
  118. $fr .= pack('V', $unc_len); // uncompressed filesize
  119. // add this entry to array
  120. $this -> datasec[] = $fr;
  121. // now add to central directory record
  122. $cdrec = "\x50\x4b\x01\x02";
  123. $cdrec .= "\x00\x00"; // version made by
  124. $cdrec .= "\x14\x00"; // version needed to extract
  125. $cdrec .= "\x00\x00"; // gen purpose bit flag
  126. $cdrec .= "\x08\x00"; // compression method
  127. $cdrec .= $hexdtime; // last mod time & date
  128. $cdrec .= pack('V', $crc); // crc32
  129. $cdrec .= pack('V', $c_len); // compressed filesize
  130. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  131. $cdrec .= pack('v', strlen($name) ); // length of filename
  132. $cdrec .= pack('v', 0 ); // extra field length
  133. $cdrec .= pack('v', 0 ); // file comment length
  134. $cdrec .= pack('v', 0 ); // disk number start
  135. $cdrec .= pack('v', 0 ); // internal file attributes
  136. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  137. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  138. $this -> old_offset += strlen($fr);
  139. $cdrec .= $name;
  140. // optional extra field, file comment goes here
  141. // save to central directory
  142. $this -> ctrl_dir[] = $cdrec;
  143. } // end of the 'addFile()' method
  144. /**
  145. * Dumps out file
  146. *
  147. * @return string the zipped file
  148. *
  149. * @access public
  150. */
  151. function file()
  152. {
  153. $data = implode('', $this -> datasec);
  154. $ctrldir = implode('', $this -> ctrl_dir);
  155. return
  156. $data .
  157. $ctrldir .
  158. $this -> eof_ctrl_dir .
  159. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  160. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  161. pack('V', strlen($ctrldir)) . // size of central dir
  162. pack('V', strlen($data)) . // offset to start of central dir
  163. "\x00\x00"; // .zip file comment length
  164. } // end of the 'file()' method
  165. /**
  166. * A Wrapper of original addFile Function
  167. *
  168. * Created By Hasin Hayder at 29th Jan, 1:29 AM
  169. *
  170. * @param array An Array of files with relative/absolute path to be added in Zip File
  171. *
  172. * @access public
  173. */
  174. function addFiles($files /*Only Pass Array*/)
  175. {
  176. foreach($files as $file)
  177. {
  178. if (is_file($file)) //directory check
  179. {
  180. $data = implode("",file($file));
  181. $this->addFile($data,$file);
  182. }
  183. }
  184. }
  185. /**
  186. * A Wrapper of original file Function
  187. *
  188. * Created By Hasin Hayder at 29th Jan, 1:29 AM
  189. *
  190. * @param string Output file name
  191. *
  192. * @access public
  193. */
  194. function output($file)
  195. {
  196. $fp=fopen($file,"w");
  197. fwrite($fp,$this->file());
  198. fclose($fp);
  199. }
  200. } // end of the 'zipfile' class
复制代码

php, zip


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
可以在PHP会话中存储哪些数据?可以在PHP会话中存储哪些数据?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,数字,数组和原始物。

您如何开始PHP会话?您如何开始PHP会话?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

什么是会话再生,如何提高安全性?什么是会话再生,如何提高安全性?May 02, 2025 am 12:15 AM

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

使用PHP会话时有哪些性能考虑?使用PHP会话时有哪些性能考虑?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP会话与Cookie有何不同?PHP会话与Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何识别用户的会话?PHP如何识别用户的会话?May 01, 2025 am 12:23 AM

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

确保PHP会议的一些最佳实践是什么?确保PHP会议的一些最佳实践是什么?May 01, 2025 am 12:22 AM

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

PHP会话文件默认存储在哪里?PHP会话文件默认存储在哪里?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境