[PHP实例] 使用PHPZip解压缩文件


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

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: