Use of PHPZip

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

PHPZip


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!