php专区

 首页 > php专区 > PHP应用 > php类库 > PHP ZIP压缩类代码 - php类库

PHP ZIP压缩类代码 - php类库

分享到:
【字体:
导读:
          本程序可以快速的实现把我们的文件利用php压缩类压缩成我们想的zip,或者rar 的压缩包,后缀名可以自定义,压缩算法是来自国外一个网站抄的.首先实例化,然后传参,两个参数,第一个关于你...

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. #  
  2. # PHPZip v1.2 by Sext (sext@neud.net) 2002-11-18 
  3. #     (Changed: 2003-03-01) 
  4. #  
  5. # Makes zip archive 
  6. # Based on "Zip file creation class", uses zLib 
  7.  
  8. class PHPZip 
  9.     function Zip($dir$zipfilename
  10.     { 
  11.         if (@function_exists('gzcompress')) 
  12.         {     
  13.             $curdir = getcwd(); 
  14.             if (is_array($dir))  
  15.             { 
  16.                     $filelist = $dir
  17.             } 
  18.             else  
  19.             { 
  20.                 $filelist = $this -> GetFileList($dir); 
  21.             } 
  22.  
  23.             if ((!emptyempty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir); 
  24.             else chdir($curdir); 
  25.  
  26.             if (count($filelist)>0) 
  27.             { 
  28.                 foreach($filelist as $filename
  29.                 { 
  30.                     if (is_file($filename)) 
  31.                     { 
  32.                         $fd = fopen ($filename"r"); 
  33.                         $content = fread ($fdfilesize ($filename)); 
  34.                         fclose ($fd); 
  35.  
  36.                         if (is_array($dir)) $filename = basename($filename); 
  37.                         $this -> addFile($content$filename); 
  38.                     } 
  39.                 } 
  40.                 $out = $this -> file(); 
  41.  
  42.                 chdir($curdir); 
  43.                 $fp = fopen($zipfilename"w"); 
  44.                 fwrite($fp$outstrlen($out)); 
  45.                 fclose($fp); 
  46.             } 
  47.             return 1; 
  48.         }  
  49.         else return 0; 
  50.     } 
  51.  
  52.     function GetFileList($dir
  53.     { 
  54.         if (file_exists($dir)) 
  55.         { 
  56.             $args = func_get_args(); 
  57.             $pref = $args[1]; 
  58.  
  59.             $dh = opendir($dir); 
  60.             while($files = readdir($dh)) 
  61.             { 
  62.                 if (($files!=".")&&($files!=".."))  
  63.                 { 
  64.                     if (is_dir($dir.$files))  
  65.                     { 
  66.                         $curdir = getcwd(); 
  67.                         chdir($dir.$files); 
  68.                         $file = array_merge($file$this -> GetFileList("""$pref$files/")); 
  69.                         chdir($curdir); 
  70.                     } 
  71.                     else $file[]=$pref.$files
  72.                 } 
  73.             } 
  74.             closedir($dh); 
  75.         } 
  76.         return $file
  77.     } 
  78.  
  79.     var $datasec      = array(); 
  80.     var $ctrl_dir     = array(); 
  81.     var $eof_ctrl_dir = "x50x4bx05x06x00x00x00x00"
  82.     var $old_offset   = 0; 
  83.  
  84.     /** 
  85.      * Converts an Unix timestamp to a four byte DOS date and time format (date 
  86.      * in high two bytes, time in low two bytes allowing magnitude comparison). 
  87.      * 
  88.      * @param  integer  the current Unix timestamp 
  89.      * 
  90.      * @return integer  the current date in a four byte DOS format 
  91.      * 
  92.      * @access private 
  93.      */ 
  94.     function unix2DosTime($unixtime = 0) { 
  95.         $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); 
  96.  
  97.         if ($timearray['year'] < 1980) { 
  98.             $timearray['year']    = 1980; 
  99.             $timearray['mon']     = 1; 
  100.             $timearray['mday']    = 1; 
  101.             $timearray['hours']   = 0; 
  102.             $timearray['minutes'] = 0; 
  103.             $timearray['seconds'] = 0; 
  104.         } // end if 
  105.  
  106.         return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | 
  107.                 ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); 
  108.     } // end of the 'unix2DosTime()' method 
  109.  
  110.     /** 
  111.      * Adds "file" to archive 
  112.      * 
  113.      * @param  string   file contents 
  114.      * @param  string   name of the file in the archive (may contains the path) 
  115.      * @param  integer  the current timestamp 
  116.      * 
  117.      * @access public 
  118.      */ 
  119.     function addFile($data$name$time = 0) 
  120.     { 
  121.         $name     = str_replace('', '/', $name); 
  122.  
  123.         $dtime    = dechex($this->unix2DosTime($time)); 
  124.         $hexdtime = 'x' . $dtime[6] . $dtime[7] 
  125.                   . 'x' . $dtime[4] . $dtime[5] 
  126.                   . 'x' . $dtime[2] . $dtime[3] 
  127.                   . 'x' . $dtime[0] . $dtime[1]; 
  128.         eval('$hexdtime = "' . $hexdtime . '";'); 
  129.  
  130.         $fr   = "x50x4bx03x04"
  131.         $fr   .= "x14x00";            // ver needed to extract 
  132.         $fr   .= "x00x00";            // gen purpose bit flag 
  133.         $fr   .= "x08x00";            // compression method 
  134.         $fr   .= $hexdtime;             // last mod time and date 
  135.  
  136.         // "local file header" segment 
  137.         $unc_len = strlen($data); 
  138.         $crc     = crc32($data); 
  139.         $zdata   = gzcompress($data); 
  140.         $c_len   = strlen($zdata); 
  141.         $zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug 
  142.         $fr      .= pack('V'$crc);             // crc32 
  143.         $fr      .= pack('V'$c_len);           // compressed filesize 
  144.         $fr      .= pack('V'$unc_len);         // uncompressed filesize 
  145.         $fr      .= pack('v'strlen($name));    // length of filename 
  146.         $fr      .= pack('v', 0);                // extra field length 
  147.         $fr      .= $name
  148.  
  149.         // "file data" segment 
  150.         $fr .= $zdata
  151.  
  152.         // "data descriptor" segment (optional but necessary if archive is not 
  153.         // served as file) 
  154.         $fr .= pack('V'$crc);                 // crc32 
  155.         $fr .= pack('V'$c_len);               // compressed filesize 
  156.         $fr .= pack('V'$unc_len);             // uncompressed filesize 
  157.  
  158.         // add this entry to array 
  159.         $this -> datasec[] = $fr
  160.         $new_offset        = strlen(implode(''$this->datasec)); 
  161.  
  162.         // now add to central directory record 
  163.         $cdrec = "x50x4bx01x02"
  164.         $cdrec .= "x00x00";                // version made by 
  165.         $cdrec .= "x14x00";                // version needed to extract 
  166.         $cdrec .= "x00x00";                // gen purpose bit flag 
  167.         $cdrec .= "x08x00";                // compression method 
  168.         $cdrec .= $hexdtime;                 // last mod time & date 
  169.         $cdrec .= pack('V'$crc);           // crc32 
  170.         $cdrec .= pack('V'$c_len);         // compressed filesize 
  171.         $cdrec .= pack('V'$unc_len);       // uncompressed filesize 
  172.         $cdrec .= pack('v'strlen($name) ); // length of filename 
  173.         $cdrec .= pack('v', 0 );             // extra field length 
  174.         $cdrec .= pack('v', 0 );             // file comment length 
  175.         $cdrec .= pack('v', 0 );             // disk number start 
  176.         $cdrec .= pack('v', 0 );             // internal file attributes 
  177.         $cdrec .= pack('V', 32 );            // external file attributes - 'archive' bit set 
  178.  
  179.         $cdrec .= pack('V'$this -> old_offset ); // relative offset of local header 
  180.         $this -> old_offset = $new_offset
  181.  
  182.         $cdrec .= $name
  183.  
  184.         // optional extra field, file comment goes here 
  185.         // save to central directory 
  186.         $this -> ctrl_dir[] = $cdrec
  187.     } // end of the 'addFile()' method 
  188.  
  189.     /** 
  190.      * Dumps out file 
  191.      * 
  192.      * @return  string  the zipped file 
  193.      * 
  194.      * @access public 
  195.      */ 
  196.     function file() 
  197.     { 
  198.         $data    = implode(''$this -> datasec); 
  199.         $ctrldir = implode(''$this -> ctrl_dir); 
  200.  
  201.         return 
  202.             $data . 
  203.             $ctrldir . 
  204.             $this -> eof_ctrl_dir . 
  205.             pack('v', sizeof($this -> ctrl_dir)) .  // total # of entries "on this disk"//开源代码phpfensi.com 
  206.             pack('v', sizeof($this -> ctrl_dir)) .  // total # of entries overall 
  207.             pack('V'strlen($ctrldir)) .           // size of central dir 
  208.             pack('V'strlen($data)) .              // offset to start of central dir 
  209.             "x00x00";                             // .zip file comment length 
  210.     } // end of the 'file()' method 
  211.  
  212. // end of the 'PHPZip' class 
  213. ?> 
分享到:
PHP文件页面缓存类 - php类库
PHP文件页面缓存类 在php中缓存分类数据库缓存,文件缓存和内存缓存,下面我来给各位同学详细介绍PHP文件缓存类实现代码,有需要了解的朋友可参考. 一个不错的PHP文件页面缓存类代码如下:
php文件缓存类文件 - php类库
php文件缓存类文件 本人给大家推一个不错php文件缓存类文件,从各方面来看本缓存类很合理并且适用于大型网站使用,php文件缓存类文件代码如下:
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……