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

这篇文章主要介绍了一个PHP的ZIP压缩类分享,需要的朋友可以参考下。

功能:将文件压缩成zip,或者rar的压缩包。后缀名可以自定义。

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

使用例子:

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

出处:http://www.phpfensi.com/php/20201127/13499.html


相关教程