VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php文件压缩之PHPZip类用法实例

这篇文章主要介绍了php文件压缩之PHPZip类用法,实例分析了PHPZip类的定义与相关使用技巧,需要的朋友可以参考下。

本文实例讲述了php文件压缩之PHPZip类用法,分享给大家供大家参考,具体如下:

  1. <?php 
  2. // 
  3. // PHPZip v1.2 by Sext (sext@neud.net)  
  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 ((!emptyempty($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.    
  115.     $dtime = dechex($this->unix2DosTime($time)); 
  116.     $hexdtime = 'x' . $dtime[6] . $dtime[7] 
  117.         . 'x' . $dtime[4] . $dtime[5] 
  118.         . 'x' . $dtime[2] . $dtime[3] 
  119.         . 'x' . $dtime[0] . $dtime[1]; 
  120.     eval('$hexdtime = "' . $hexdtime . '";'); 
  121.     $fr = "x50x4bx03x04"; 
  122.     $fr .= "x14x00";     // ver needed to extract 
  123.     $fr .= "x00x00";     // gen purpose bit flag 
  124.     $fr .= "x08x00";     // compression method 
  125.     $fr .= $hexdtime;     // last mod time and date 
  126.    
  127.     // "local file header" segment 
  128.     $unc_len = strlen($data); 
  129.     $crc   = crc32($data); 
  130.     $zdata = gzcompress($data); 
  131.     $c_len = strlen($zdata); 
  132.     $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug 
  133.     $fr  .= pack('V', $crc);     // crc32 
  134.     $fr  .= pack('V', $c_len);      // compressed filesize 
  135.     $fr  .= pack('V', $unc_len);    // uncompressed filesize 
  136.     $fr  .= pack('v', strlen($name)); // length of filename 
  137.     $fr  .= pack('v', 0);       // extra field length 
  138.     $fr  .= $name; 
  139.     // "file data" segment 
  140.     $fr .= $zdata; 
  141.     // "data descriptor" segment (optional but necessary if archive is not 
  142.     // served as file) 
  143.     $fr .= pack('V', $crc);         // crc32 
  144.     $fr .= pack('V', $c_len);       // compressed filesize 
  145.     $fr .= pack('V', $unc_len);     // uncompressed filesize 
  146.     // add this entry to array 
  147.     $this -> datasec[] = $fr; 
  148.     $new_offset    = strlen(implode('', $this->datasec)); 
  149.     // now add to central directory record 
  150.     $cdrec = "x50x4bx01x02"; 
  151.     $cdrec .= "x00x00";       // version made by 
  152.     $cdrec .= "x14x00";       // version needed to extract 
  153.     $cdrec .= "x00x00";       // gen purpose bit flag 
  154.     $cdrec .= "x08x00";       // compression method 
  155.     $cdrec .= $hexdtime;         // last mod time & date 
  156.     $cdrec .= pack('V', $crc);      // crc32 
  157.     $cdrec .= pack('V', $c_len);    // compressed filesize 
  158.     $cdrec .= pack('V', $unc_len);  // uncompressed filesize 
  159.     $cdrec .= pack('v', strlen($name) ); // length of filename 
  160.     $cdrec .= pack('v', 0 );     // extra field length 
  161.     $cdrec .= pack('v', 0 );     // file comment length 
  162.     $cdrec .= pack('v', 0 );     // disk number start 
  163.     $cdrec .= pack('v', 0 );     // internal file attributes 
  164.     $cdrec .= pack('V', 32 );     // external file attributes - 'archive' bit set 
  165.     $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header 
  166.     $this -> old_offset = $new_offset; 
  167.     $cdrec .= $name; 
  168.     // optional extra field, file comment goes here 
  169.     // save to central directory 
  170.     $this -> ctrl_dir[] = $cdrec; 
  171. } // end of the 'addFile()' method 
  172. /** 
  173.   * Dumps out file 
  174.   * 
  175.   * @return  string  the zipped file 
  176.   * 
  177.   * @access public 
  178.   */ 
  179. function file() 
  180. { 
  181.     $data = implode('', $this -> datasec); 
  182.     $ctrldir = implode('', $this -> ctrl_dir); 
  183.    
  184.     return 
  185.      $data . 
  186.      $ctrldir . 
  187.      $this -> eof_ctrl_dir . 
  188.      pack('v', sizeof($this -> ctrl_dir)) .  // total # of entries "on this disk" 
  189.      pack('v', sizeof($this -> ctrl_dir)) .  // total # of entries overall 
  190.      pack('V', strlen($ctrldir)) .      // size of central dir 
  191.      pack('V', strlen($data)) .       // offset to start of central dir 
  192.      "x00x00";               // .zip file comment length 
  193. } // end of the 'file()' method 
  194. } // end of the 'PHPZip' class 
  195. ?> 

使用方法:

  1. $z = new PHPZip(); //新建立一个zip的类 
  2. //方法一: 
  3. $z -> Zip("", "out1.zip"); //添加当前目录和子目录下的所有档案 
  4. //方法二: 
  5. $files=array('1.txt','gb.txt'); 
  6. $files[]='5.txt'; 
  7. $z -> Zip($files, "out2.zip"); //添加文件列表 
  8. //方法三: 
  9. $z -> Zip("/usr/local/sext/", "out3.zip"); //添加指定目录
  10.  

出处:http://www.phpfensi.com/php/20210528/16024.html


相关教程