PHPZip的使用

WBOY
发布: 2016-07-25 08:42:25
原创
838 人浏览过
  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'] $timearray['year'] = 1980;
  83. $timearray['mon'] = 1;
  84. $timearray['mday'] = 1;
  85. $timearray['hours'] = 0;
  86. $timearray['minutes'] = 0;
  87. $timearray['seconds'] = 0;
  88. } // end if
  89. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);
  90. } // end of the 'unix2DosTime()' method
  91. /**
  92. * Adds "file" to archive
  93. *
  94. * @param string file contents
  95. * @param string name of the file in the archive (may contains the path)
  96. * @param integer the current timestamp
  97. *
  98. * @access public
  99. */
  100. function addFile($data, $name, $time = 0)
  101. {
  102. $name = str_replace('', '/', $name);
  103. $dtime = dechex($this->unix2DosTime($time));
  104. $hexdtime = 'x' . $dtime[6] . $dtime[7]
  105. . 'x' . $dtime[4] . $dtime[5]
  106. . 'x' . $dtime[2] . $dtime[3]
  107. . 'x' . $dtime[0] . $dtime[1];
  108. eval('$hexdtime = "' . $hexdtime . '";');
  109. $fr = "x50x4bx03x04";
  110. $fr .= "x14x00"; // ver needed to extract
  111. $fr .= "x00x00"; // gen purpose bit flag
  112. $fr .= "x08x00"; // compression method
  113. $fr .= $hexdtime; // last mod time and date
  114. // "local file header" segment
  115. $unc_len = strlen($data);
  116. $crc = crc32($data);
  117. $zdata = gzcompress($data);
  118. $c_len = strlen($zdata);
  119. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  120. $fr .= pack('V', $crc); // crc32
  121. $fr .= pack('V', $c_len); // compressed filesize
  122. $fr .= pack('V', $unc_len); // uncompressed filesize
  123. $fr .= pack('v', strlen($name)); // length of filename
  124. $fr .= pack('v', 0); // extra field length
  125. $fr .= $name;
  126. // "file data" segment
  127. $fr .= $zdata;
  128. // "data descriptor" segment (optional but necessary if archive is not
  129. // served as file)
  130. $fr .= pack('V', $crc); // crc32
  131. $fr .= pack('V', $c_len); // compressed filesize
  132. $fr .= pack('V', $unc_len); // uncompressed filesize
  133. // add this entry to array
  134. $this -> datasec[] = $fr;
  135. $new_offset = strlen(implode('', $this->datasec));
  136. // now add to central directory record
  137. $cdrec = "x50x4bx01x02";
  138. $cdrec .= "x00x00"; // version made by
  139. $cdrec .= "x14x00"; // version needed to extract
  140. $cdrec .= "x00x00"; // gen purpose bit flag
  141. $cdrec .= "x08x00"; // compression method
  142. $cdrec .= $hexdtime; // last mod time & date
  143. $cdrec .= pack('V', $crc); // crc32
  144. $cdrec .= pack('V', $c_len); // compressed filesize
  145. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  146. $cdrec .= pack('v', strlen($name) ); // length of filename
  147. $cdrec .= pack('v', 0 ); // extra field length
  148. $cdrec .= pack('v', 0 ); // file comment length
  149. $cdrec .= pack('v', 0 ); // disk number start
  150. $cdrec .= pack('v', 0 ); // internal file attributes
  151. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  152. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  153. $this -> old_offset = $new_offset;
  154. $cdrec .= $name;
  155. // optional extra field, file comment goes here
  156. // save to central directory
  157. $this -> ctrl_dir[] = $cdrec;
  158. } // end of the 'addFile()' method
  159. /**
  160. * Dumps out file
  161. *
  162. * @return string the zipped file
  163. *
  164. * @access public
  165. */
  166. function file()
  167. {
  168. $data = implode('', $this -> datasec);
  169. $ctrldir = implode('', $this -> ctrl_dir);
  170. return
  171. $data .
  172. $ctrldir .
  173. $this -> eof_ctrl_dir .
  174. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  175. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  176. pack('V', strlen($ctrldir)) . // size of central dir
  177. pack('V', strlen($data)) . // offset to start of central dir
  178. "x00x00"; // .zip file comment length
  179. } // end of the 'file()' method
  180. } // end of the 'PHPZip' class
  181. ?>
  182. //使用方法
  183. $z = new PHPZip(); //新建立一个zip的类
  184. 方法一:
  185. $z -> Zip("", "out1.zip"); //添加当前目录和子目录下的所有档案
  186. 方法二:
  187. $files=array('1.txt','gb.txt');
  188. $files[]='5.txt';
  189. $z -> Zip($files, "out2.zip"); //添加文件列表
  190. 方法三:
  191. $z -> Zip("/usr/local/sext/", "out3.zip"); //添加指定目录
  192. ?>
复制代码

PHPZip


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板