一个PHP的ZIP压缩类分享

这篇文章主要介绍了一个PHP的ZIP压缩类分享,需要的朋友可以参考下。

功能:将文件压缩成zip,或者rar的压缩包。后缀名可以自定义。

使用方法:首先实例化,然后传参。两个参数。第一个关于你文件地址的一个Array。第二个是要你要保存的压缩包文件的绝对地址。

使用例子:

  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. <?php
  2. #
  3. # PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18
  4. # (Changed: 2003-03-01)
  5. #
  6. # Makes zip archive
  7. #
  8. # Based on "Zip file creation class", uses zLib
  9. #
  10. #
  11. class PHPZip
  12. {
  13. function Zip($dir, $zipfilename)
  14. {
  15. if (@function_exists('gzcompress'))
  16. {
  17. $curdir = getcwd();
  18. if (is_array($dir))
  19. {
  20. $filelist = $dir;
  21. }
  22. else
  23. {
  24. $filelist = $this -> GetFileList($dir);
  25. }
  26. if ((!emptyempty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir);
  27. else chdir($curdir);
  28. if (count($filelist)>0)
  29. {
  30. foreach($filelist as $filename)
  31. {
  32. if (is_file($filename))
  33. {
  34. $fd = fopen ($filename, "r");
  35. $content = fread ($fd, filesize ($filename));
  36. fclose ($fd);
  37. if (is_array($dir)) $filename = basename($filename);
  38. $this -> addFile($content, $filename);
  39. }
  40. }
  41. $out = $this -> file();
  42. chdir($curdir);
  43. $fp = fopen($zipfilename, "w");
  44. fwrite($fp, $out, strlen($out));
  45. fclose($fp);
  46. }
  47. return 1;
  48. }
  49. else return 0;
  50. }
  51. function GetFileList($dir)
  52. {
  53. if (file_exists($dir))
  54. {
  55. $args = func_get_args();
  56. $pref = $args[1];
  57. $dh = opendir($dir);
  58. while($files = readdir($dh))
  59. {
  60. if (($files!=".")&&($files!=".."))
  61. {
  62. if (is_dir($dir.$files))
  63. {
  64. $curdir = getcwd();
  65. chdir($dir.$files);
  66. $file = array_merge($file, $this -> GetFileList("", "$pref$files/"));
  67. chdir($curdir);
  68. }
  69. else $file[]=$pref.$files;
  70. }
  71. }
  72. closedir($dh);
  73. }
  74. return $file;
  75. }
  76. var $datasec = array();
  77. var $ctrl_dir = array();
  78. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  79. var $old_offset = 0;
  80. /**
  81. * Converts an Unix timestamp to a four byte DOS date and time format (date
  82. * in high two bytes, time in low two bytes allowing magnitude comparison).
  83. *
  84. * @param integer the current Unix timestamp
  85. *
  86. * @return integer the current date in a four byte DOS format
  87. *
  88. * @access private
  89. */
  90. function unix2DosTime($unixtime = 0) {
  91. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  92. if ($timearray['year'] < 1980) {
  93. $timearray['year'] = 1980;
  94. $timearray['mon'] = 1;
  95. $timearray['mday'] = 1;
  96. $timearray['hours'] = 0;
  97. $timearray['minutes'] = 0;
  98. $timearray['seconds'] = 0;
  99. } // end if
  100. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  101. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  102. } // end of the 'unix2DosTime()' method
  103. /**
  104. * Adds "file" to archive
  105. *
  106. * @param string file contents
  107. * @param string name of the file in the archive (may contains the path)
  108. * @param integer the current timestamp
  109. *
  110. * @access public
  111. */
  112. function addFile($data, $name, $time = 0)
  113. {
  114. $name = str_replace('\\', '/', $name);
  115. $dtime = dechex($this->unix2DosTime($time));
  116. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  117. <a href="http://wutransfer.com/western-union-locations-in-russia-taiynsha/">Western union point</a> . '\x' . $dtime[4] . $dtime[5]
  118. . '\x' . $dtime[2] . $dtime[3]
  119. . '\x' . $dtime[0] . $dtime[1];
  120. eval('$hexdtime = "' . $hexdtime . '";');
  121. $fr = "\x50\x4b\x03\x04";
  122. $fr .= "\x14\x00"; // ver needed to extract
  123. $fr .= "\x00\x00"; // gen purpose bit flag
  124. $fr .= "\x08\x00"; // compression method
  125. $fr .= $hexdtime; // last mod time and date
  126. // "local file header" segment
  127. $unc_len = strlen($data);
  128. $crc = crc32($data);
  129. $zdata = gzcompress($data);
  130. $c_len = strlen($zdata);
  131. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  132. $fr .= pack('V', $crc); // crc32
  133. $fr .= pack('V', $c_len); // compressed filesize
  134. $fr .= pack('V', $unc_len); // uncompressed filesize
  135. $fr .= pack('v', strlen($name)); // length of filename
  136. $fr .= pack('v', 0); // extra field length
  137. $fr .= $name;
  138. // "file data" segment
  139. $fr .= $zdata;
  140. // "data descriptor" segment (optional but necessary if archive is not
  141. // served as file)
  142. $fr .= pack('V', $crc); // crc32
  143. $fr .= pack('V', $c_len); // compressed filesize
  144. $fr .= pack('V', $unc_len); // uncompressed filesize
  145. // add this entry to array
  146. $this -> datasec[] = $fr;
  147. $new_offset = strlen(implode('', $this->datasec));
  148. // now add to central directory record
  149. $cdrec = "\x50\x4b\x01\x02";
  150. $cdrec .= "\x00\x00"; // version made by
  151. $cdrec .= "\x14\x00"; // version needed to extract
  152. $cdrec .= "\x00\x00"; // gen purpose bit flag
  153. $cdrec .= "\x08\x00"; // compression method
  154. $cdrec .= $hexdtime; // last mod time & date
  155. $cdrec .= pack('V', $crc); // crc32
  156. $cdrec .= pack('V', $c_len); // compressed filesize
  157. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  158. $cdrec .= pack('v', strlen($name) ); // length of filename
  159. $cdrec .= pack('v', 0 ); // extra field length
  160. $cdrec .= pack('v', 0 ); // file comment length
  161. $cdrec .= pack('v', 0 ); // disk number start
  162. $cdrec .= pack('v', 0 ); // internal file attributes
  163. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  164. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  165. $this -> old_offset = $new_offset;
  166. $cdrec .= $name;
  167. // optional extra field, file comment goes here
  168. // save to central directory
  169. $this -> ctrl_dir[] = $cdrec;
  170. } // end of the 'addFile()' method
  171. /**
  172. * Dumps out file
  173. *
  174. * @return string the zipped file
  175. *
  176. * @access public
  177. */
  178. function file()
  179. {
  180. $data = implode('', $this -> datasec);
  181. $ctrldir = implode('', $this -> ctrl_dir);
  182. return
  183. $data .
  184. $ctrldir .
  185. $this -> eof_ctrl_dir .
  186. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  187. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  188. pack('V', strlen($ctrldir)) . // size of central dir
  189. pack('V', strlen($data)) . // offset to start of central dir
  190. "\x00\x00"; // .zip file comment length
  191. } // end of the 'file()' method
  192. //phpfensi.com
  193. } // end of the 'PHPZip' class
  194. ?>