Heim >Backend-Entwicklung >PHP-Tutorial >php压缩文件帮助类

php压缩文件帮助类

WBOY
WBOYOriginal
2016-07-25 08:45:341385Durchsuche
  1. /*
  2. File name: /include/zip.php
  3. Author: Horace 2009/04/15
  4. */
  5. class PHPZip{
  6. var $dirInfo = array("0","0");
  7. var $rootDir = '';
  8. var $datasec = array();
  9. var $ctrl_dir = array();
  10. var $eof_ctrl_dir = "/x50/x4b/x05/x06/x00/x00/x00/x00";
  11. var $old_offset = 0;
  12. function downloadZip(){
  13. createZip($dir, $zipfilename, true);
  14. }
  15. function createZip($dir, $zipfilename, $autoDownload = false){
  16. if (@function_exists('gzcompress')){
  17. @set_time_limit("0");
  18. if (is_array($dir)){
  19. $fd = fopen ($dir, "r");
  20. $fileValue = fread ($fd, filesize ($filename));
  21. fclose ($fd);
  22. if (is_array($dir)) $filename = basename($dir);
  23. $this -> addFile($fileValue, "$filename");
  24. }else{
  25. $this->dirTree($dir,$dir);
  26. }
  27. $zipfilenametemp = time().$zipfilename;
  28. $out = $this -> filezip();
  29. $fp = fopen($zipfilenametemp, "w");
  30. fwrite($fp, $out, strlen($out));
  31. fclose($fp);
  32. $filesize = filesize($zipfilenametemp);
  33. if ($filesize if($autoDownload){
  34. header("Content-type: application/octet-stream");
  35. header("Content-disposition: attachment; filename=".$zipfilename);
  36. }
  37. echo $this -> filezip();
  38. }else{
  39. echo "create zip error!";
  40. }
  41. unlink($zipfilenametemp);
  42. }
  43. }
  44. //get dir tree..
  45. function dirTree($directory,$rootDir){
  46. global $_SERVER,$dirInfo,$rootDir;
  47. $fileDir=$rootDir;
  48. $myDir=dir($directory);
  49. while($file=$myDir->read()){
  50. if(is_dir("$directory/$file") and $file!="." and $file!=".."){
  51. $dirInfo[0]++;
  52. $rootDir ="$fileDir$file/";
  53. $this -> addFile('', "$rootDir");
  54. //go on n's folders
  55. $this->dirTree("$directory/$file",$rootDir);
  56. }else{
  57. if($file!="." and $file!=".."){
  58. $dirInfo[1]++;
  59. //$fd = fopen ("$directory/$file", "r");
  60. $fileValue = file_get_contents("$directory/$file");
  61. //fclose ($fd);
  62. $this -> addFile($fileValue, "$fileDir$file");
  63. }
  64. }
  65. }
  66. $myDir->close();
  67. }
  68. function unix2DosTime($unixtime = 0) {
  69. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  70. if ($timearray['year'] $timearray['year'] = 1980;
  71. $timearray['mon'] = 1;
  72. $timearray['mday'] = 1;
  73. $timearray['hours'] = 0;
  74. $timearray['minutes'] = 0;
  75. $timearray['seconds'] = 0;
  76. } // end if
  77. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);
  78. }
  79. function addFile($data, $name, $time = 0){
  80. $name = str_replace('//', '/', $name);
  81. $dtime = dechex($this->unix2DosTime($time));
  82. $hexdtime = '/x' . $dtime[6] . $dtime[7]
  83. . '/x' . $dtime[4] . $dtime[5]
  84. . '/x' . $dtime[2] . $dtime[3]
  85. . '/x' . $dtime[0] . $dtime[1];
  86. eval('$hexdtime = "' . $hexdtime . '";');
  87. $fr = "/x50/x4b/x03/x04";
  88. $fr .= "/x14/x00"; // ver needed to extract
  89. $fr .= "/x00/x00"; // gen purpose bit flag
  90. $fr .= "/x08/x00"; // compression method
  91. $fr .= $hexdtime; // last mod time and date
  92. // "local file header" segment
  93. $unc_len = strlen($data);
  94. $crc = crc32($data);
  95. $zdata = gzcompress($data);
  96. $c_len = strlen($zdata);
  97. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  98. $fr .= pack('V', $crc); // crc32
  99. $fr .= pack('V', $c_len); // compressed filesize
  100. $fr .= pack('V', $unc_len); // uncompressed filesize
  101. $fr .= pack('v', strlen($name)); // length of filename
  102. $fr .= pack('v', 0); // extra field length
  103. $fr .= $name;
  104. // "file data" segment
  105. $fr .= $zdata;
  106. // "data descriptor" segment (optional but necessary if archive is not
  107. // served as file)
  108. $fr .= pack('V', $crc); // crc32
  109. $fr .= pack('V', $c_len); // compressed filesize
  110. $fr .= pack('V', $unc_len); // uncompressed filesize
  111. // add this entry to array
  112. $this -> datasec[] = $fr;
  113. $new_offset = strlen(implode('', $this->datasec));
  114. // now add to central directory record
  115. $cdrec = "/x50/x4b/x01/x02";
  116. $cdrec .= "/x00/x00"; // version made by
  117. $cdrec .= "/x14/x00"; // version needed to extract
  118. $cdrec .= "/x00/x00"; // gen purpose bit flag
  119. $cdrec .= "/x08/x00"; // compression method
  120. $cdrec .= $hexdtime; // last mod time & date
  121. $cdrec .= pack('V', $crc); // crc32
  122. $cdrec .= pack('V', $c_len); // compressed filesize
  123. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  124. $cdrec .= pack('v', strlen($name) ); // length of filename
  125. $cdrec .= pack('v', 0 ); // extra field length
  126. $cdrec .= pack('v', 0 ); // file comment length
  127. $cdrec .= pack('v', 0 ); // disk number start
  128. $cdrec .= pack('v', 0 ); // internal file attributes
  129. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  130. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  131. $this -> old_offset = $new_offset;
  132. $cdrec .= $name;
  133. // optional extra field, file comment goes here
  134. // save to central directory
  135. $this -> ctrl_dir[] = $cdrec;
  136. }
  137. function filezip(){
  138. $data = implode('', $this -> datasec);
  139. $ctrldir = implode('', $this -> ctrl_dir);
  140. return
  141. $data .
  142. $ctrldir .
  143. $this -> eof_ctrl_dir .
  144. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  145. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  146. pack('V', strlen($ctrldir)) . // size of central dir
  147. pack('V', strlen($data)) . // offset to start of central dir
  148. "/x00/x00"; // .zip file comment length
  149. }
  150. }
  151. ?>
  152. 还有另外一个比较简单的
  153. [php] view plaincopy
  154. class createzip
  155. {
  156. /* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数
  157. * @$files 数组类型 实例array("1.jpg","2.jpg");
  158. * @destination 目标文件的路径 如"c:/androidyue.zip"
  159. * @$overwrite 是否为覆盖与目标文件相同的文件
  160. * @Recorded By Androidyue
  161. * @Blog:http://thinkblog.sinaapp.com
  162. */
  163. function create_zip($files = array(),$destination = '',$overwrite = false)
  164. {
  165. //如果zip文件已经存在并且设置为不重写返回false
  166. if(file_exists($destination) && !$overwrite) { return false; }
  167. $valid_files = array();
  168. //if files were passed in...
  169. //获取到真实有效的文件名
  170. if(is_array($files)) {
  171. //cycle through each file
  172. foreach($files as $file) {
  173. //make sure the file exists
  174. if(file_exists($file)) {
  175. $valid_files[] = $file;
  176. }
  177. }
  178. }
  179. //如果存在真实有效的文件
  180. if(count($valid_files))
  181. {
  182. //create the archive
  183. $zip = new ZipArchive();
  184. //打开文件 如果文件已经存在则覆盖,如果没有则创建
  185. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  186. return false;
  187. }
  188. //向压缩文件中添加文件
  189. foreach($valid_files as $file) {
  190. $zip->addFile($file,$file);
  191. }
  192. //关闭文件
  193. $zip->close();
  194. //检测文件是否存在
  195. return file_exists($destination);
  196. }
  197. else{
  198. //如果没有真实有效的文件返回false
  199. return false;
  200. }
  201. }
  202. }
  203. /****
  204. //测试函数
  205. $files=array('temp.php','test.php');
  206. create_zip($files, 'myzipfile.zip', true);
  207. ****/
  208. ?>
复制代码

压缩文件, php


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn