VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP ZIP压缩类代码

本程序可以快速的实现把我们的文件利用php压缩类压缩成我们想的zip,或者rar 的压缩包,后缀名可以自定义,压缩算法是来自国外一个网站抄的.

首先实例化,然后传参,两个参数,第一个关于你文件地址的一个Array,第二个是要你要保存的压缩包文件的绝对地址.

For example,代码如下:

  1. $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt"); 
  2.        $z = new PHPZip(); 
  3.        //$randomstr = random(8); 
  4.        $zipfile = TEMP."/photocome_".$groupid.".zip"; 
  5.  
  6.        $z->Zip($zipfiles, $zipfile); 

添加文件列表PHP的ZIP压缩类如下,代码如下:

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

出处:http://www.phpfensi.com/php/20140828/4902.html


相关教程