搜索
首页后端开发php教程php压缩文件帮助类

  1. /*
  2. File name: /include/zip.php
  3. Author: Horace 2009/04/15
  4. */
  5. class PHPZip{
  6. var $dirInfo = array("0","0");
  7. var $rootDir = '';
  8. var $datasec = array();
  9. var $ctrl_dir = array();
  10. var $eof_ctrl_dir = "/x50/x4b/x05/x06/x00/x00/x00/x00";
  11. var $old_offset = 0;
  12. function downloadZip(){
  13. createZip($dir, $zipfilename, true);
  14. }
  15. function createZip($dir, $zipfilename, $autoDownload = false){
  16. if (@function_exists('gzcompress')){
  17. @set_time_limit("0");
  18. if (is_array($dir)){
  19. $fd = fopen ($dir, "r");
  20. $fileValue = fread ($fd, filesize ($filename));
  21. fclose ($fd);
  22. if (is_array($dir)) $filename = basename($dir);
  23. $this -> addFile($fileValue, "$filename");
  24. }else{
  25. $this->dirTree($dir,$dir);
  26. }
  27. $zipfilenametemp = time().$zipfilename;
  28. $out = $this -> filezip();
  29. $fp = fopen($zipfilenametemp, "w");
  30. fwrite($fp, $out, strlen($out));
  31. fclose($fp);
  32. $filesize = filesize($zipfilenametemp);
  33. if ($filesize if($autoDownload){
  34. header("Content-type: application/octet-stream");
  35. header("Content-disposition: attachment; filename=".$zipfilename);
  36. }
  37. echo $this -> filezip();
  38. }else{
  39. echo "create zip error!";
  40. }
  41. unlink($zipfilenametemp);
  42. }
  43. }
  44. //get dir tree..
  45. function dirTree($directory,$rootDir){
  46. global $_SERVER,$dirInfo,$rootDir;
  47. $fileDir=$rootDir;
  48. $myDir=dir($directory);
  49. while($file=$myDir->read()){
  50. if(is_dir("$directory/$file") and $file!="." and $file!=".."){
  51. $dirInfo[0]++;
  52. $rootDir ="$fileDir$file/";
  53. $this -> addFile('', "$rootDir");
  54. //go on n's folders
  55. $this->dirTree("$directory/$file",$rootDir);
  56. }else{
  57. if($file!="." and $file!=".."){
  58. $dirInfo[1]++;
  59. //$fd = fopen ("$directory/$file", "r");
  60. $fileValue = file_get_contents("$directory/$file");
  61. //fclose ($fd);
  62. $this -> addFile($fileValue, "$fileDir$file");
  63. }
  64. }
  65. }
  66. $myDir->close();
  67. }
  68. function unix2DosTime($unixtime = 0) {
  69. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  70. if ($timearray['year'] $timearray['year'] = 1980;
  71. $timearray['mon'] = 1;
  72. $timearray['mday'] = 1;
  73. $timearray['hours'] = 0;
  74. $timearray['minutes'] = 0;
  75. $timearray['seconds'] = 0;
  76. } // end if
  77. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);
  78. }
  79. function addFile($data, $name, $time = 0){
  80. $name = str_replace('//', '/', $name);
  81. $dtime = dechex($this->unix2DosTime($time));
  82. $hexdtime = '/x' . $dtime[6] . $dtime[7]
  83. . '/x' . $dtime[4] . $dtime[5]
  84. . '/x' . $dtime[2] . $dtime[3]
  85. . '/x' . $dtime[0] . $dtime[1];
  86. eval('$hexdtime = "' . $hexdtime . '";');
  87. $fr = "/x50/x4b/x03/x04";
  88. $fr .= "/x14/x00"; // ver needed to extract
  89. $fr .= "/x00/x00"; // gen purpose bit flag
  90. $fr .= "/x08/x00"; // compression method
  91. $fr .= $hexdtime; // last mod time and date
  92. // "local file header" segment
  93. $unc_len = strlen($data);
  94. $crc = crc32($data);
  95. $zdata = gzcompress($data);
  96. $c_len = strlen($zdata);
  97. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  98. $fr .= pack('V', $crc); // crc32
  99. $fr .= pack('V', $c_len); // compressed filesize
  100. $fr .= pack('V', $unc_len); // uncompressed filesize
  101. $fr .= pack('v', strlen($name)); // length of filename
  102. $fr .= pack('v', 0); // extra field length
  103. $fr .= $name;
  104. // "file data" segment
  105. $fr .= $zdata;
  106. // "data descriptor" segment (optional but necessary if archive is not
  107. // served as file)
  108. $fr .= pack('V', $crc); // crc32
  109. $fr .= pack('V', $c_len); // compressed filesize
  110. $fr .= pack('V', $unc_len); // uncompressed filesize
  111. // add this entry to array
  112. $this -> datasec[] = $fr;
  113. $new_offset = strlen(implode('', $this->datasec));
  114. // now add to central directory record
  115. $cdrec = "/x50/x4b/x01/x02";
  116. $cdrec .= "/x00/x00"; // version made by
  117. $cdrec .= "/x14/x00"; // version needed to extract
  118. $cdrec .= "/x00/x00"; // gen purpose bit flag
  119. $cdrec .= "/x08/x00"; // compression method
  120. $cdrec .= $hexdtime; // last mod time & date
  121. $cdrec .= pack('V', $crc); // crc32
  122. $cdrec .= pack('V', $c_len); // compressed filesize
  123. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  124. $cdrec .= pack('v', strlen($name) ); // length of filename
  125. $cdrec .= pack('v', 0 ); // extra field length
  126. $cdrec .= pack('v', 0 ); // file comment length
  127. $cdrec .= pack('v', 0 ); // disk number start
  128. $cdrec .= pack('v', 0 ); // internal file attributes
  129. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  130. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  131. $this -> old_offset = $new_offset;
  132. $cdrec .= $name;
  133. // optional extra field, file comment goes here
  134. // save to central directory
  135. $this -> ctrl_dir[] = $cdrec;
  136. }
  137. function filezip(){
  138. $data = implode('', $this -> datasec);
  139. $ctrldir = implode('', $this -> ctrl_dir);
  140. return
  141. $data .
  142. $ctrldir .
  143. $this -> eof_ctrl_dir .
  144. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  145. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  146. pack('V', strlen($ctrldir)) . // size of central dir
  147. pack('V', strlen($data)) . // offset to start of central dir
  148. "/x00/x00"; // .zip file comment length
  149. }
  150. }
  151. ?>
  152. 还有另外一个比较简单的
  153. [php] view plaincopy
  154. class createzip
  155. {
  156. /* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数
  157. * @$files 数组类型 实例array("1.jpg","2.jpg");
  158. * @destination 目标文件的路径 如"c:/androidyue.zip"
  159. * @$overwrite 是否为覆盖与目标文件相同的文件
  160. * @Recorded By Androidyue
  161. * @Blog:http://thinkblog.sinaapp.com
  162. */
  163. function create_zip($files = array(),$destination = '',$overwrite = false)
  164. {
  165. //如果zip文件已经存在并且设置为不重写返回false
  166. if(file_exists($destination) && !$overwrite) { return false; }
  167. $valid_files = array();
  168. //if files were passed in...
  169. //获取到真实有效的文件名
  170. if(is_array($files)) {
  171. //cycle through each file
  172. foreach($files as $file) {
  173. //make sure the file exists
  174. if(file_exists($file)) {
  175. $valid_files[] = $file;
  176. }
  177. }
  178. }
  179. //如果存在真实有效的文件
  180. if(count($valid_files))
  181. {
  182. //create the archive
  183. $zip = new ZipArchive();
  184. //打开文件 如果文件已经存在则覆盖,如果没有则创建
  185. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  186. return false;
  187. }
  188. //向压缩文件中添加文件
  189. foreach($valid_files as $file) {
  190. $zip->addFile($file,$file);
  191. }
  192. //关闭文件
  193. $zip->close();
  194. //检测文件是否存在
  195. return file_exists($destination);
  196. }
  197. else{
  198. //如果没有真实有效的文件返回false
  199. return false;
  200. }
  201. }
  202. }
  203. /****
  204. //测试函数
  205. $files=array('temp.php','test.php');
  206. create_zip($files, 'myzipfile.zip', true);
  207. ****/
  208. ?>
复制代码

压缩文件, php


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何检查PHP会话是否已经开始?如何检查PHP会话是否已经开始?Apr 30, 2025 am 12:20 AM

在PHP中,可以使用session_status()或session_id()来检查会话是否已启动。1)使用session_status()函数,如果返回PHP_SESSION_ACTIVE,则会话已启动。2)使用session_id()函数,如果返回非空字符串,则会话已启动。这两种方法都能有效地检查会话状态,选择使用哪种方法取决于PHP版本和个人偏好。

描述一个场景,其中使用会话在Web应用程序中至关重要。描述一个场景,其中使用会话在Web应用程序中至关重要。Apr 30, 2025 am 12:16 AM

sessionsarevitalinwebapplications,尤其是在commercePlatform之前。

如何管理PHP中的并发会话访问?如何管理PHP中的并发会话访问?Apr 30, 2025 am 12:11 AM

在PHP中管理并发会话访问可以通过以下方法:1.使用数据库存储会话数据,2.采用Redis或Memcached,3.实施会话锁定策略。这些方法有助于确保数据一致性和提高并发性能。

使用PHP会话的局限性是什么?使用PHP会话的局限性是什么?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

解释负载平衡如何影响会话管理以及如何解决。解释负载平衡如何影响会话管理以及如何解决。Apr 29, 2025 am 12:42 AM

负载均衡会影响会话管理,但可以通过会话复制、会话粘性和集中式会话存储解决。1.会话复制在服务器间复制会话数据。2.会话粘性将用户请求定向到同一服务器。3.集中式会话存储使用独立服务器如Redis存储会话数据,确保数据共享。

说明会话锁定的概念。说明会话锁定的概念。Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

有其他PHP会议的选择吗?有其他PHP会议的选择吗?Apr 29, 2025 am 12:36 AM

PHP会话的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。1.Cookies通过在客户端存储数据来管理会话,简单但安全性低。2.Token-basedAuthentication使用令牌验证用户,安全性高但需额外逻辑。3.Database-basedSessions将数据存储在数据库中,扩展性好但可能影响性能。4.Redis/Memcached使用分布式缓存提高性能和扩展性,但需额外配

在PHP的上下文中定义'会话劫持”一词。在PHP的上下文中定义'会话劫持”一词。Apr 29, 2025 am 12:33 AM

Sessionhijacking是指攻击者通过获取用户的sessionID来冒充用户。防范方法包括:1)使用HTTPS加密通信;2)验证sessionID的来源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

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

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

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

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

禅工作室 13.0.1

禅工作室 13.0.1

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