php ZIP compression class example sharing

WBOY
Release: 2016-07-25 09:13:07
Original
726 people have browsed it

Function: Compress files into zip or rar archives. The suffix name can be customized. Usage: First instantiate, then pass parameters. Two parameters. The first one is an Array of file addresses. The second is the absolute address of the compressed package file to be saved.

How to call the php zip compression class:

  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); //Add file list
Copy code

PHP’s ZIP compression class:

  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 = "x50x4bx05x06x00x00x00x00";
  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'] < 1980) {

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

  99. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |

  100. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  101. }// end of the 'unix2DosTime()' method

  102. /**

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

  114. $dtime = dechex($this->unix2DosTime($time));

  115. $hexdtime = 'x' . $dtime[6] . $dtime[7]
  116. Western union point . 'x' . $dtime[4] . $dtime[5]
  117. . 'x' . $dtime[2] . $dtime[3]
  118. . 'x' . $dtime[0] . $dtime[1];
  119. eval('$hexdtime = "' . $hexdtime . '";');

  120. $fr = "x50x4bx03x04";

  121. $fr .= "x14x00"; // ver needed to extract
  122. $fr .= "x00x00"; // gen purpose bit flag
  123. $fr .= "x08x00"; // compression method
  124. $fr .= $hexdtime; // last mod time and date

  125. // "local file header" segment

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

  137. // "file data" segment

  138. $fr .= $zdata;

  139. // "data descriptor" segment (optional but necessary if archive is not

  140. // served as file)
  141. $fr .= pack('V', $crc); // crc32
  142. $fr .= pack('V', $c_len); // compressed filesize
  143. $fr .= pack('V', $unc_len); // uncompressed filesize

  144. // add this entry to array

  145. $this -> datasec[] = $fr;
  146. $new_offset = strlen(implode('', $this->datasec));

  147. // now add to central directory record

  148. $cdrec = "x50x4bx01x02";
  149. $cdrec .= "x00x00"; // version made by
  150. $cdrec .= "x14x00"; // version needed to extract
  151. $cdrec .= "x00x00"; // gen purpose bit flag
  152. $cdrec .= "x08x00"; // compression method
  153. $cdrec .= $hexdtime; // last mod time & date
  154. $cdrec .= pack('V', $crc); // crc32
  155. $cdrec .= pack('V', $c_len); // compressed filesize
  156. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  157. $cdrec .= pack('v', strlen($name) ); // length of filename
  158. $cdrec .= pack('v', 0 ); // extra field length
  159. $cdrec .= pack('v', 0 ); // file comment length
  160. $cdrec .= pack('v', 0 ); // disk number start
  161. $cdrec .= pack('v', 0 ); // internal file attributes
  162. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set

  163. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header

  164. $this -> old_offset = $new_offset;

  165. $cdrec .= $name;

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

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

  170. /**

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

  181. return

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

  191. } // end of the 'PHPZip' class

  192. ?>

Copy Code

The PHP zip compression class implemented by foreigners, I personally feel that it is good. It contains a lot of application skills of PHP practical functions, especially the application of function pack, crc32, gzcompress and other functions, which are worth learning and reference.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!