搜索
首页后端开发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
在Laravel中使用Flash会话数据在Laravel中使用Flash会话数据Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP记录:PHP日志分析的最佳实践PHP记录:PHP日志分析的最佳实践Mar 10, 2025 pm 02:32 PM

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

php中的卷曲:如何在REST API中使用PHP卷曲扩展php中的卷曲:如何在REST API中使用PHP卷曲扩展Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

简化的HTTP响应在Laravel测试中模拟了简化的HTTP响应在Laravel测试中模拟了Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12个最佳PHP聊天脚本在Codecanyon上的12个最佳PHP聊天脚本Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

解释PHP中晚期静态结合的概念。解释PHP中晚期静态结合的概念。Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

自定义/扩展框架:如何添加自定义功能。自定义/扩展框架:如何添加自定义功能。Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SecLists

SecLists

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

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器