PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php ZIP压缩类实例分享

原创
2016-07-25 09:13:07 567浏览

功能:将文件压缩成zip,或者rar的压缩包。后缀名可以自定义。 使用方法:首先实例化,然后传参。两个参数。 第一个关于文件地址的一个Array。 第二个是要保存的压缩包文件的绝对地址。

php zip压缩类的调用方法:

  1. $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt");
  2. $z = new PHPZip();
  3. //$randomstr = random(8);
  4. $zipfile = TEMP."/photocome_".$groupid.".zip";
  5. $z->Zip($zipfiles, $zipfile); //添加文件列表
复制代码

PHP的ZIP压缩类:

  1. #

  2. # PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18
  3. # (Changed: 2003-03-01)
  4. #
  5. # Makes zip archive
  6. #
  7. # Based on "Zip file creation class", uses zLib
  8. #
  9. #
  10. class PHPZip
  11. {
  12. function Zip($dir, $zipfilename)
  13. {
  14. if (@function_exists('gzcompress'))
  15. {
  16. $curdir = getcwd();
  17. if (is_array($dir))
  18. {
  19. $filelist = $dir;
  20. }
  21. else
  22. {
  23. $filelist = $this -> GetFileList($dir);
  24. }
  25. if ((!empty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir);

  26. else chdir($curdir);
  27. if (count($filelist)>0)

  28. {
  29. foreach($filelist as $filename)
  30. {
  31. if (is_file($filename))
  32. {
  33. $fd = fopen ($filename, "r");
  34. $content = fread ($fd, filesize ($filename));
  35. fclose ($fd);
  36. if (is_array($dir)) $filename = basename($filename);

  37. $this -> addFile($content, $filename);
  38. }
  39. }
  40. $out = $this -> file();
  41. chdir($curdir);

  42. $fp = fopen($zipfilename, "w");
  43. fwrite($fp, $out, strlen($out));
  44. fclose($fp);
  45. }
  46. return 1;
  47. }
  48. else return 0;
  49. }
  50. function GetFileList($dir)

  51. {
  52. if (file_exists($dir))
  53. {
  54. $args = func_get_args();
  55. $pref = $args[1];
  56. $dh = opendir($dir);

  57. while($files = readdir($dh))
  58. {
  59. if (($files!=".")&&($files!=".."))
  60. {
  61. if (is_dir($dir.$files))
  62. {
  63. $curdir = getcwd();
  64. chdir($dir.$files);
  65. $file = array_merge($file, $this -> GetFileList("", "$pref$files/"));
  66. chdir($curdir);
  67. }
  68. else $file[]=$pref.$files;
  69. }
  70. }
  71. closedir($dh);
  72. }
  73. return $file;
  74. }
  75. var $datasec = array();

  76. var $ctrl_dir = array();
  77. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  78. var $old_offset = 0;
  79. /**

  80. * Converts an Unix timestamp to a four byte DOS date and time format (date
  81. * in high two bytes, time in low two bytes allowing magnitude comparison).
  82. *
  83. * @param integer the current Unix timestamp
  84. *
  85. * @return integer the current date in a four byte DOS format
  86. *
  87. * @access private
  88. */
  89. function unix2DosTime($unixtime = 0) {
  90. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  91. if ($timearray['year'] $timearray['year'] = 1980;

  92. $timearray['mon'] = 1;
  93. $timearray['mday'] = 1;
  94. $timearray['hours'] = 0;
  95. $timearray['minutes'] = 0;
  96. $timearray['seconds'] = 0;
  97. } // end if
  98. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);

  99. } // end of the 'unix2DosTime()' method
  100. /**

  101. * Adds "file" to archive
  102. *
  103. * @param string file contents
  104. * @param string name of the file in the archive (may contains the path)
  105. * @param integer the current timestamp
  106. *
  107. * @access public
  108. */
  109. function addFile($data, $name, $time = 0)
  110. {
  111. $name = str_replace('\\', '//m.sbmmt.com/m/', $name);
  112. $dtime = dechex($this->unix2DosTime($time));

  113. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  114. Western union point . '\x' . $dtime[4] . $dtime[5]
  115. . '\x' . $dtime[2] . $dtime[3]
  116. . '\x' . $dtime[0] . $dtime[1];
  117. eval('$hexdtime = "' . $hexdtime . '";');
  118. $fr = "\x50\x4b\x03\x04";

  119. $fr .= "\x14\x00"; // ver needed to extract
  120. $fr .= "\x00\x00"; // gen purpose bit flag
  121. $fr .= "\x08\x00"; // compression method
  122. $fr .= $hexdtime; // last mod time and date
  123. // "local file header" segment

  124. $unc_len = strlen($data);
  125. $crc = crc32($data);
  126. $zdata = gzcompress($data);
  127. $c_len = strlen($zdata);
  128. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  129. $fr .= pack('V', $crc); // crc32
  130. $fr .= pack('V', $c_len); // compressed filesize
  131. $fr .= pack('V', $unc_len); // uncompressed filesize
  132. $fr .= pack('v', strlen($name)); // length of filename
  133. $fr .= pack('v', 0); // extra field length
  134. $fr .= $name;
  135. // "file data" segment

  136. $fr .= $zdata;
  137. // "data descriptor" segment (optional but necessary if archive is not

  138. // served as file)
  139. $fr .= pack('V', $crc); // crc32
  140. $fr .= pack('V', $c_len); // compressed filesize
  141. $fr .= pack('V', $unc_len); // uncompressed filesize
  142. // add this entry to array

  143. $this -> datasec[] = $fr;
  144. $new_offset = strlen(implode('', $this->datasec));
  145. // now add to central directory record

  146. $cdrec = "\x50\x4b\x01\x02";
  147. $cdrec .= "\x00\x00"; // version made by
  148. $cdrec .= "\x14\x00"; // version needed to extract
  149. $cdrec .= "\x00\x00"; // gen purpose bit flag
  150. $cdrec .= "\x08\x00"; // compression method
  151. $cdrec .= $hexdtime; // last mod time & date
  152. $cdrec .= pack('V', $crc); // crc32
  153. $cdrec .= pack('V', $c_len); // compressed filesize
  154. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  155. $cdrec .= pack('v', strlen($name) ); // length of filename
  156. $cdrec .= pack('v', 0 ); // extra field length
  157. $cdrec .= pack('v', 0 ); // file comment length
  158. $cdrec .= pack('v', 0 ); // disk number start
  159. $cdrec .= pack('v', 0 ); // internal file attributes
  160. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  161. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header

  162. $this -> old_offset = $new_offset;
  163. $cdrec .= $name;

  164. // optional extra field, file comment goes here

  165. // save to central directory
  166. $this -> ctrl_dir[] = $cdrec;
  167. } // end of the 'addFile()' method
  168. /**

  169. * Dumps out file
  170. *
  171. * @return string the zipped file
  172. *
  173. * @access public
  174. */
  175. function file()
  176. {
  177. $data = implode('', $this -> datasec);
  178. $ctrldir = implode('', $this -> ctrl_dir);
  179. return

  180. $data .
  181. $ctrldir .
  182. $this -> eof_ctrl_dir .
  183. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  184. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  185. pack('V', strlen($ctrldir)) . // size of central dir
  186. pack('V', strlen($data)) . // offset to start of central dir
  187. "\x00\x00"; // .zip file comment length
  188. } // end of the 'file()' method
  189. } // end of the 'PHPZip' class

  190. ?>
复制代码

老外实现的php zip压缩类,个人感觉还是不错的,里面有大量php实用函数的应用技巧,特别是函数pack、crc32、gzcompress等函数的应用,值得学习借鉴。



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。