当前位置:Gxlcms > PHP教程 > 使用PHPZip解压缩文件

使用PHPZip解压缩文件

时间:2021-07-01 10:21:17 帮助过:17人阅读

  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. . '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. ?>
  193. //使用方法
  194. $z = new PHPZip(); //新建立一个zip的类
  195. 方法一:
  196. $z -> Zip("", "out1.zip"); //添加当前目录和子目录下的所有档案
  197. 方法二:
  198. $files=array('1.txt','gb.txt');
  199. $files[]='5.txt';
  200. $z -> Zip($files, "out2.zip"); //添加文件列表
  201. 方法三:
  202. $z -> Zip("/usr/local/sext/", "out3.zip"); //添加指定目录
  203. ?>

解压缩, PHPZip

人气教程排行