1. [代码]PHP压缩与解压Zip(PHPZip类) 

PHP代码
  1. <?php  
  2.     class PHPZip  
  3.     {  
  4.         private $ctrl_dir     = array();  
  5.         private $datasec      = array();  
  6.    
  7.    
  8.         /********************************************************** 
  9.          * 压缩部分 
  10.          **********************************************************/  
  11.         // ------------------------------------------------------ //  
  12.         // #遍历指定文件夹  
  13.         //  
  14.         // $archive  = new PHPZip();  
  15.         // $filelist = $archive->visitFile(文件夹路径);  
  16.         // print "当前文件夹的文件:<p>\r\n";  
  17.         // foreach($filelist as $file)  
  18.         //     printf("%s<br>\r\n", $file);  
  19.         // ------------------------------------------------------ //  
  20.         var $fileList = array();  
  21.         public function visitFile($path)  
  22.         {  
  23.             global $fileList;  
  24.             $path = str_replace("\\", "/", $path); 
  25.             $fdir = dir($path); 
  26.           
  27.             while(($file = $fdir->read()) !== false) 
  28.             { 
  29.                 if($file == '.' || $file == '..'){ continue; } 
  30.           
  31.                 $pathSub    = preg_replace("*/{2,}*", "/", $path."/".$file);  // 替换多个反斜杠 
  32.                 $fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub; 
  33.                 if(is_dir($pathSub)){ $this->visitFile($pathSub); } 
  34.             } 
  35.             $fdir->close(); 
  36.             return $fileList; 
  37.         } 
  38.           
  39.           
  40.         private function unix2DosTime($unixtime = 0) 
  41.         { 
  42.             $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); 
  43.       
  44.             if($timearray['year'] < 1980) 
  45.             { 
  46.                 $timearray['year']    = 1980; 
  47.                 $timearray['mon']     = 1; 
  48.                 $timearray['mday']    = 1; 
  49.                 $timearray['hours']   = 0; 
  50.                 $timearray['minutes'] = 0; 
  51.                 $timearray['seconds'] = 0; 
  52.             } 
  53.       
  54.             return (  ($timearray['year'] - 1980) << 25) 
  55.                     | ($timearray['mon'] << 21) 
  56.                     | ($timearray['mday'] << 16) 
  57.                     | ($timearray['hours'] << 11) 
  58.                     | ($timearray['minutes'] << 5) 
  59.                     | ($timearray['seconds'] >> 1); 
  60.         } 
  61.           
  62.           
  63.         var $old_offset = 0; 
  64.         private function addFile($data, $filename, $time = 0) 
  65.         { 
  66.             $filename = str_replace('\\', '/', $filename); 
  67.       
  68.             $dtime    = dechex($this->unix2DosTime($time)); 
  69.             $hexdtime = '\x' . $dtime[6] . $dtime[7] 
  70.                       . '\x' . $dtime[4] . $dtime[5] 
  71.                       . '\x' . $dtime[2] . $dtime[3] 
  72.                       . '\x' . $dtime[0] . $dtime[1]; 
  73.             eval('$hexdtime = "' . $hexdtime . '";'); 
  74.       
  75.             $fr       = "\x50\x4b\x03\x04"; 
  76.             $fr      .= "\x14\x00"; 
  77.             $fr      .= "\x00\x00"; 
  78.             $fr      .= "\x08\x00"; 
  79.             $fr      .= $hexdtime; 
  80.             $unc_len  = strlen($data); 
  81.             $crc      = crc32($data); 
  82.             $zdata    = gzcompress($data); 
  83.             $c_len    = strlen($zdata); 
  84.             $zdata    = substr(substr($zdata, 0, strlen($zdata) - 4), 2); 
  85.             $fr      .= pack('V', $crc); 
  86.             $fr      .= pack('V', $c_len); 
  87.             $fr      .= pack('V', $unc_len); 
  88.             $fr      .= pack('v', strlen($filename)); 
  89.             $fr      .= pack('v', 0); 
  90.             $fr      .= $filename; 
  91.       
  92.             $fr      .= $zdata; 
  93.       
  94.             $fr      .= pack('V', $crc); 
  95.             $fr      .= pack('V', $c_len); 
  96.             $fr      .= pack('V', $unc_len); 
  97.       
  98.             $this->datasec[] = $fr; 
  99.             $new_offset      = strlen(implode('', $this->datasec)); 
  100.       
  101.             $cdrec  = "\x50\x4b\x01\x02"; 
  102.             $cdrec .= "\x00\x00"; 
  103.             $cdrec .= "\x14\x00"; 
  104.             $cdrec .= "\x00\x00"; 
  105.             $cdrec .= "\x08\x00"; 
  106.             $cdrec .= $hexdtime; 
  107.             $cdrec .= pack('V', $crc); 
  108.             $cdrec .= pack('V', $c_len); 
  109.             $cdrec .= pack('V', $unc_len); 
  110.             $cdrec .= pack('v', strlen($filename) ); 
  111.             $cdrec .= pack('v', 0 ); 
  112.             $cdrec .= pack('v', 0 ); 
  113.             $cdrec .= pack('v', 0 ); 
  114.             $cdrec .= pack('v', 0 ); 
  115.             $cdrec .= pack('V', 32 ); 
  116.       
  117.             $cdrec .= pack('V', $this->old_offset ); 
  118.             $this->old_offset = $new_offset; 
  119.       
  120.             $cdrec .= $filename; 
  121.             $this->ctrl_dir[] = $cdrec; 
  122.         } 
  123.           
  124.           
  125.         var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; 
  126.         private function file() 
  127.         { 
  128.             $data    = implode('', $this->datasec); 
  129.             $ctrldir = implode('', $this->ctrl_dir); 
  130.       
  131.             return   $data 
  132.                    . $ctrldir 
  133.                    . $this->eof_ctrl_dir 
  134.                    . pack('v', sizeof($this->ctrl_dir)) 
  135.                    . pack('v', sizeof($this->ctrl_dir)) 
  136.                    . pack('V', strlen($ctrldir)) 
  137.                    . pack('V', strlen($data)) 
  138.                    . "\x00\x00"; 
  139.         } 
  140.       
  141.           
  142.         // ------------------------------------------------------ // 
  143.         // #压缩到服务器 
  144.         // 
  145.         // $archive = new PHPZip(); 
  146.         // $archive->Zip("需压缩的文件所在目录", "ZIP压缩文件名");  
  147.         // ------------------------------------------------------ // 
  148.         public function Zip($dir, $saveName) 
  149.         { 
  150.             if(@!function_exists('gzcompress')){ return; } 
  151.       
  152.             ob_end_clean(); 
  153.             $filelist = $this->visitFile($dir); 
  154.             if(count($filelist) == 0){ return; } 
  155.       
  156.             foreach($filelist as $file) 
  157.             { 
  158.                 if(!file_exists($file) || !is_file($file)){ continue; } 
  159.                   
  160.                 $fd       = fopen($file, "rb"); 
  161.                 $content  = @fread($fd, filesize($file)); 
  162.                 fclose($fd); 
  163.   
  164.                 // 1.删除$dir的字符(./folder/file.txt删除./folder/) 
  165.                 // 2.如果存在/就删除(/file.txt删除/) 
  166.                 $file = substr($file, strlen($dir)); 
  167.                 if(substr($file, 0, 1) == "\\" || substr($file, 0, 1) == "/"){ $file = substr($file, 1); } 
  168.                   
  169.                 $this->addFile($content, $file); 
  170.             } 
  171.             $out = $this->file(); 
  172.       
  173.             $fp = fopen($saveName, "wb"); 
  174.             fwrite($fp, $out, strlen($out)); 
  175.             fclose($fp); 
  176.         } 
  177.       
  178.       
  179.         // ------------------------------------------------------ // 
  180.         // #压缩并直接下载 
  181.         // 
  182.         // $archive = new PHPZip(); 
  183.         // $archive->ZipAndDownload("需压缩的文件所在目录"); 
  184.         // ------------------------------------------------------ // 
  185.         public function ZipAndDownload($dir) 
  186.         { 
  187.             if(@!function_exists('gzcompress')){ return; } 
  188.       
  189.             ob_end_clean(); 
  190.             $filelist = $this->visitFile($dir); 
  191.             if(count($filelist) == 0){ return; } 
  192.       
  193.             foreach($filelist as $file) 
  194.             { 
  195.                 if(!file_exists($file) || !is_file($file)){ continue; } 
  196.                   
  197.                 $fd       = fopen($file, "rb"); 
  198.                 $content  = @fread($fd, filesize($file)); 
  199.                 fclose($fd); 
  200.       
  201.                 // 1.删除$dir的字符(./folder/file.txt删除./folder/) 
  202.                 // 2.如果存在/就删除(/file.txt删除/) 
  203.                 $file = substr($file, strlen($dir)); 
  204.                 if(substr($file, 0, 1) == "\\" || substr($file, 0, 1) == "/"){ $file = substr($file, 1); } 
  205.                   
  206.                 $this->addFile($content, $file); 
  207.             } 
  208.             $out = $this->file(); 
  209.       
  210.             @header('Content-Encoding: none'); 
  211.             @header('Content-Type: application/zip'); 
  212.             @header('Content-Disposition: attachment ; filename=Farticle'.date("YmdHis", time()).'.zip'); 
  213.             @header('Pragma: no-cache'); 
  214.             @header('Expires: 0'); 
  215.             print($out); 
  216.         } 
  217.           
  218.           
  219.           
  220.           
  221.           
  222.         /********************************************************** 
  223.          * 解压部分 
  224.          **********************************************************/ 
  225.         // ------------------------------------------------------ // 
  226.         // ReadCentralDir($zip, $zipfile) 
  227.         // $zip是经过@fopen($zipfile, 'rb')打开的 
  228.         // $zipfile是zip文件的路径 
  229.         // ------------------------------------------------------ // 
  230.         private function ReadCentralDir($zip, $zipfile) 
  231.         { 
  232.             $size     = filesize($zipfile); 
  233.             $max_size = ($size < 277) ? $size : 277; 
  234.               
  235.             @fseek($zip, $size - $max_size); 
  236.             $pos   = ftell($zip); 
  237.             $bytes = 0x00000000; 
  238.               
  239.             while($pos < $size) 
  240.             { 
  241.                 $byte  = @fread($zip, 1); 
  242.                 $bytes = ($bytes << 8) | Ord($byte); 
  243.                 $pos++; 
  244.                 if($bytes == 0x504b0506){ break; } 
  245.             } 
  246.               
  247.             $data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', fread($zip, 18)); 
  248.   
  249.             $centd['comment']      = ($data['comment_size'] != 0) ? fread($zip, $data['comment_size']) : '';  // 注释 
  250.             $centd['entries']      = $data['entries']; 
  251.             $centd['disk_entries'] = $data['disk_entries']; 
  252.             $centd['offset']       = $data['offset']; 
  253.             $centd['disk_start']   = $data['disk_start']; 
  254.             $centd['size']         = $data['size']; 
  255.             $centd['disk']         = $data['disk']; 
  256.             return $centd; 
  257.         } 
  258.           
  259.           
  260.         private function ReadCentralFileHeaders($zip) 
  261.         { 
  262.             $binary_data = fread($zip, 46); 
  263.             $header      = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data); 
  264.   
  265.             $header['filename'] = ($header['filename_len'] != 0) ? fread($zip, $header['filename_len']) : ''; 
  266.             $header['extra']    = ($header['extra_len']    != 0) ? fread($zip, $header['extra_len'])    : ''; 
  267.             $header['comment']  = ($header['comment_len']  != 0) ? fread($zip, $header['comment_len'])  : ''; 
  268.   
  269.       
  270.             if($header['mdate'] && $header['mtime']) 
  271.             { 
  272.                 $hour    = ($header['mtime']  & 0xF800) >> 11; 
  273.                 $minute  = ($header['mtime']  & 0x07E0) >> 5; 
  274.                 $seconde = ($header['mtime']  & 0x001F) * 2; 
  275.                 $year    = (($header['mdate'] & 0xFE00) >> 9) + 1980; 
  276.                 $month   = ($header['mdate']  & 0x01E0) >> 5; 
  277.                 $day     = $header['mdate']   & 0x001F; 
  278.                 $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year); 
  279.             } else { 
  280.                 $header['mtime'] = time(); 
  281.             } 
  282.             $header['stored_filename'] = $header['filename']; 
  283.             $header['status'] = 'ok'; 
  284.             if(substr($header['filename'], -1) == '/'){ $header['external'] = 0x41FF0010; }  // 判断是否文件夹 
  285.             return $header; 
  286.         } 
  287.       
  288.       
  289.         private function ReadFileHeader($zip) 
  290.         { 
  291.             $binary_data = fread($zip, 30); 
  292.             $data        = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data); 
  293.       
  294.             $header['filename']        = fread($zip, $data['filename_len']); 
  295.             $header['extra']           = ($data['extra_len'] != 0) ? fread($zip, $data['extra_len']) : ''; 
  296.             $header['compression']     = $data['compression']; 
  297.             $header['size']            = $data['size']; 
  298.             $header['compressed_size'] = $data['compressed_size']; 
  299.             $header['crc']             = $data['crc']; 
  300.             $header['flag']            = $data['flag']; 
  301.             $header['mdate']           = $data['mdate']; 
  302.             $header['mtime']           = $data['mtime']; 
  303.       
  304.             if($header['mdate'] && $header['mtime']){ 
  305.                 $hour    = ($header['mtime']  & 0xF800) >> 11; 
  306.                 $minute  = ($header['mtime']  & 0x07E0) >> 5; 
  307.                 $seconde = ($header['mtime']  & 0x001F) * 2; 
  308.                 $year    = (($header['mdate'] & 0xFE00) >> 9) + 1980; 
  309.                 $month   = ($header['mdate']  & 0x01E0) >> 5; 
  310.                 $day     = $header['mdate']   & 0x001F; 
  311.                 $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year); 
  312.             }else{ 
  313.                 $header['mtime'] = time(); 
  314.             } 
  315.       
  316.             $header['stored_filename'] = $header['filename']; 
  317.             $header['status']          = "ok"; 
  318.             return $header; 
  319.         } 
  320.       
  321.       
  322.         private function ExtractFile($header, $to, $zip) 
  323.         { 
  324.             $header = $this->readfileheader($zip); 
  325.               
  326.             if(substr($to, -1) != "/"){ $to .= "/"; } 
  327.             if([email protected]_dir($to)){ @mkdir($to, 0777); } 
  328.               
  329.             $pth = explode("/", dirname($header['filename'])); 
  330.             for($i=0; isset($pth[$i]); $i++){ 
  331.                 if(!$pth[$i]){ continue; } 
  332.                 $pthss .= $pth[$i]."/"; 
  333.                 if(!is_dir($to.$pthss)){ @mkdir($to.$pthss, 0777); } 
  334.             } 
  335.               
  336.             if(!($header['external'] == 0x41FF0010) && !($header['external'] == 16)) 
  337.             { 
  338.                 if($header['compression'] == 0) 
  339.                 { 
  340.                     $fp = @fopen($to.$header['filename'], 'wb'); 
  341.                     if(!$fp){ return(-1); } 
  342.                     $size = $header['compressed_size']; 
  343.                       
  344.                     while($size != 0) 
  345.                     { 
  346.                         $read_size   = ($size < 2048 ? $size : 2048); 
  347.                         $buffer      = fread($zip, $read_size); 
  348.                         $binary_data = pack('a'.$read_size, $buffer); 
  349.                         @fwrite($fp, $binary_data, $read_size); 
  350.                         $size       -= $read_size; 
  351.                     } 
  352.                     fclose($fp); 
  353.                     touch($to.$header['filename'], $header['mtime']); 
  354.                   
  355.                 }else{ 
  356.                       
  357.                     $fp = @fopen($to.$header['filename'].'.gz', 'wb'); 
  358.                     if(!$fp){ return(-1); } 
  359.                     $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']), Chr(0x00), time(), Chr(0x00), Chr(3)); 
  360.                       
  361.                     fwrite($fp, $binary_data, 10); 
  362.                     $size = $header['compressed_size']; 
  363.                       
  364.                     while($size != 0) 
  365.                     { 
  366.                         $read_size   = ($size < 1024 ? $size : 1024); 
  367.                         $buffer      = fread($zip, $read_size); 
  368.                         $binary_data = pack('a'.$read_size, $buffer); 
  369.                         @fwrite($fp, $binary_data, $read_size); 
  370.                         $size       -= $read_size; 
  371.                     } 
  372.                       
  373.                     $binary_data = pack('VV', $header['crc'], $header['size']); 
  374.                     fwrite($fp, $binary_data, 8); 
  375.                     fclose($fp); 
  376.                       
  377.                     $gzp = @gzopen($to.$header['filename'].'.gz', 'rb') or die("Cette archive est compress!"); 
  378.                       
  379.                     if(!$gzp){ return(-2); } 
  380.                     $fp = @fopen($to.$header['filename'], 'wb'); 
  381.                     if(!$fp){ return(-1); } 
  382.                     $size = $header['size']; 
  383.                       
  384.                     while($size != 0) 
  385.                     { 
  386.                         $read_size   = ($size < 2048 ? $size : 2048); 
  387.                         $buffer      = gzread($gzp, $read_size); 
  388.                         $binary_data = pack('a'.$read_size, $buffer); 
  389.                         @fwrite($fp, $binary_data, $read_size); 
  390.                         $size       -= $read_size; 
  391.                     } 
  392.                     fclose($fp); gzclose($gzp); 
  393.                       
  394.                     touch($to.$header['filename'], $header['mtime']); 
  395.                     @unlink($to.$header['filename'].'.gz'); 
  396.                 } 
  397.             } 
  398.             return true; 
  399.         } 
  400.           
  401.           
  402.         // ------------------------------------------------------ // 
  403.         // #解压文件 
  404.         // 
  405.         // $archive   = new PHPZip(); 
  406.         // $zipfile   = "ZIP压缩文件名"; 
  407.         // $savepath  = "解压缩目录名"; 
  408.         // $zipfile   = $unzipfile; 
  409.         // $savepath  = $unziptarget; 
  410.         // $array     = $archive->GetZipInnerFilesInfo($zipfile); 
  411.         // $filecount = 0; 
  412.         // $dircount  = 0; 
  413.         // $failfiles = array(); 
  414.         // set_time_limit(0);  // 修改为不限制超时时间(默认为30秒) 
  415.         // 
  416.         // for($i=0; $i<count($array); $i++) { 
  417.         //     if($array[$i][folder] == 0){ 
  418.         //         if($archive->unZip($zipfile, $savepath, $i) > 0){ 
  419.         //             $filecount++; 
  420.         //         }else{ 
  421.         //             $failfiles[] = $array[$i][filename]; 
  422.         //         } 
  423.         //     }else{ 
  424.         //         $dircount++; 
  425.         //     } 
  426.         // } 
  427.         // set_time_limit(30); 
  428.         //printf("文件夹:%d    解压文件:%d    失败:%d<br>\r\n", $dircount, $filecount, count($failfiles)); 
  429.         //if(count($failfiles) > 0){ 
  430.         //    foreach($failfiles as $file){ 
  431.         //        printf("·%s<br>\r\n", $file); 
  432.         //    } 
  433.         //} 
  434.         // ------------------------------------------------------ // 
  435.         public function unZip($zipfile, $to, $index = Array(-1)) 
  436.         { 
  437.             $ok  = 0; 
  438.             $zip = @fopen($zipfile, 'rb'); 
  439.             if(!$zip){ return(-1); } 
  440.               
  441.             $cdir      = $this->ReadCentralDir($zip, $zipfile); 
  442.             $pos_entry = $cdir['offset']; 
  443.               
  444.             if(!is_array($index)){ $index = array($index); } 
  445.             for($i=0; $index[$i]; $i++) 
  446.             { 
  447.                 if(intval($index[$i]) != $index[$i] || $index[$i] > $cdir['entries']) 
  448.                 { 
  449.                     return(-1); 
  450.                 } 
  451.             } 
  452.               
  453.             for($i=0; $i<$cdir['entries']; $i++) 
  454.             { 
  455.                 @fseek($zip, $pos_entry); 
  456.                 $header          = $this->ReadCentralFileHeaders($zip); 
  457.                 $header['index'] = $i; 
  458.                 $pos_entry       = ftell($zip); 
  459.                 @rewind($zip); 
  460.                 fseek($zip, $header['offset']); 
  461.                 if(in_array("-1", $index) || in_array($i, $index)) 
  462.                 { 
  463.                     $stat[$header['filename']] = $this->ExtractFile($header, $to, $zip); 
  464.                 } 
  465.             } 
  466.               
  467.             fclose($zip); 
  468.             return $stat; 
  469.         } 
  470.           
  471.           
  472.           
  473.           
  474.           
  475.         /********************************************************** 
  476.          * 其它部分 
  477.          **********************************************************/ 
  478.         // ------------------------------------------------------ // 
  479.         // #获取被压缩文件的信息 
  480.         // 
  481.         // $archive = new PHPZip(); 
  482.         // $array = $archive->GetZipInnerFilesInfo(ZIP压缩文件名); 
  483.         // for($i=0; $i<count($array); $i++) { 
  484.         //     printf("<b>·%s</b><br>\r\n", $array[$i][filename]); 
  485.         //     foreach($array[$i] as $key => $value) 
  486.         //         printf("%s => %s<br>\r\n", $key, $value); 
  487.         //     print "\r\n<p>------------------------------------<p>\r\n\r\n"; 
  488.         // } 
  489.         // ------------------------------------------------------ // 
  490.         public function GetZipInnerFilesInfo($zipfile) 
  491.         { 
  492.             $zip = @fopen($zipfile, 'rb'); 
  493.             if(!$zip){ return(0); } 
  494.             $centd = $this->ReadCentralDir($zip, $zipfile); 
  495.               
  496.             @rewind($zip); 
  497.             @fseek($zip, $centd['offset']); 
  498.             $ret = array(); 
  499.   
  500.             for($i=0; $i<$centd['entries']; $i++) 
  501.             { 
  502.                 $header          = $this->ReadCentralFileHeaders($zip); 
  503.                 $header['index'] = $i; 
  504.                 $info = array( 
  505.                     'filename'        => $header['filename'],                   // 文件名 
  506.                     'stored_filename' => $header['stored_filename'],            // 压缩后文件名 
  507.                     'size'            => $header['size'],                       // 大小 
  508.                     'compressed_size' => $header['compressed_size'],            // 压缩后大小 
  509.                     'crc'             => strtoupper(dechex($header['crc'])),    // CRC32 
  510.                     'mtime'           => date("Y-m-d H:i:s",$header['mtime']),  // 文件修改时间 
  511.                     'comment'         => $header['comment'],                    // 注释 
  512.                     'folder'          => ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0,  // 是否为文件夹 
  513.                     'index'           => $header['index'],                      // 文件索引 
  514.                     'status'          => $header['status']                      // 状态 
  515.                 ); 
  516.                 $ret[] = $info; 
  517.                 unset($header); 
  518.             } 
  519.             fclose($zip); 
  520.             return $ret; 
  521.         } 
  522.           
  523.           
  524.         // ------------------------------------------------------ // 
  525.         // #获取压缩文件的注释 
  526.         // 
  527.         // $archive = new PHPZip(); 
  528.         // echo $archive->GetZipComment(ZIP压缩文件名); 
  529.         // ------------------------------------------------------ // 
  530.         public function GetZipComment($zipfile) 
  531.         { 
  532.             $zip = @fopen($zipfile, 'rb'); 
  533.             if(!$zip){ return(0); } 
  534.             $centd = $this->ReadCentralDir($zip, $zipfile); 
  535.             fclose($zip); 
  536.             return $centd[comment]; 
  537.         } 
  538.     } 
  539. ?> 
  540.   
  541.   
  542.   
  543.   
  544.   
  545. <form method="post"> 
  546.   压缩到服务器: 
  547.   <br>------------------<br> 
  548.   压缩目录:     <input name="zipdir" type="text" id="zipdir" />(例如./folder)<br> 
  549.   另存为路径和文件名:<input name="savename" type="text" id="savename" />(例如./folder/file.zip)(需要填写扩展名)<br> 
  550.   <input name="zip" type="submit" id="zip" value="压缩" /><br> 
  551.   <br><br><br> 
  552.     
  553.   压缩并下载 
  554.   <br>------------------<br> 
  555.   文件所在目录:<input name="zipdowndir" type="text" id="zipdowndir" />(例如./folder)<br> 
  556.   <input name="zipdown" type="submit" id="zipdown" value="压缩并下载" /> 
  557.   <br><br><br> 
  558.     
  559.   在线解压zip 
  560.   <br>------------------<br> 
  561.   文件: <input name="unzipfile" type="text" id="unzipfile" />(例如./folder/file.zip)<br> 
  562.   解压到:<input name="unziptarget" type="text" id="unziptarget" />(例如./folder)<br> 
  563.   <input name="unzip" type="submit" id="unzip" value="解压" /> 
  564.   <br><br><br> 
  565.     
  566.   读取压缩文件内部文件信息和注释 
  567.   <br>------------------<br> 
  568.   文件:<input name="readfile" type="text" id="readfile" />(例如./folder/file.zip)<br> 
  569.   <input name="readfileinfo" type="submit" id="readfileinfo" value="读取内部文件信息" /> 
  570.   <input name="readcomment" type="submit" id="readcomment" value="读取注释" /> 
  571. </form> 
  572. <? 
  573.     echo "<p><p>\r\n"; 
  574.     echo "<br>--------------------------<br>\r\n"; 
  575.     echo "显示信息:\r\n"; 
  576.     echo "<br>--------------------------<br>\r\n"; 
  577.       
  578.     $archive  = new PHPZip(); 
  579.     if(!empty($zip)) 
  580.     { 
  581.         $archive->Zip($zipdir, $savename); 
  582.     } 
  583.     elseif(!empty($zipdown)) 
  584.     { 
  585.         $archive->ZipAndDownload($zipdowndir); 
  586.     } 
  587.     elseif(!empty($unzip)) 
  588.     { 
  589.         $zipfile   = $unzipfile; 
  590.         $savepath  = $unziptarget; 
  591.         $array     = $archive->GetZipInnerFilesInfo($zipfile); 
  592.         $filecount = 0; 
  593.         $dircount  = 0; 
  594.         $failfiles = array(); 
  595.         set_time_limit(0);  // 修改为不限制超时时间(默认为30秒) 
  596.           
  597.         for($i=0; $i<count($array); $i++) { 
  598.             if($array[$i][folder] == 0){ 
  599.                 if($archive->unZip($zipfile, $savepath, $i) > 0){ 
  600.                     $filecount++; 
  601.                 }else{ 
  602.                     $failfiles[] = $array[$i][filename]; 
  603.                 } 
  604.             }else{ 
  605.                 $dircount++; 
  606.             } 
  607.         } 
  608.   
  609.              set_time_limit(30); 
  610.   
  611. printf("文件夹:%d    解压文件:%d    失败:%d<br>\r\n", $dircount, $filecount, count($failfiles)); 
  612.         if(count($failfiles) > 0){ 
  613.             foreach($failfiles as $file){ 
  614.                 printf("·%s<br>\r\n", $file); 
  615.             } 
  616.         } 
  617.     } 
  618.     elseif(!empty($readfileinfo)) 
  619.     { 
  620.         $array = $archive->GetZipInnerFilesInfo($readfile); 
  621.         for($i=0; $i<count($array); $i++) { 
  622.             printf("<b>·%s</b><br>\r\n", $array[$i][filename]); 
  623.             foreach($array[$i] as $key => $value) 
  624.                 printf("%s => %s<br>\r\n", $key, $value); 
  625.             print "\r\n<p>------------------------------------<p>\r\n\r\n"; 
  626.         } 
  627.     } 
  628.     elseif(!empty($readcomment)) 
  629.     { 
  630.         $comment = $archive->GetZipComment($readfile); 
  631.         printf("%s<br>\r\n", $comment);  
  632.     }  
  633. ?>  

       2. [图片] 截图

详解PHP压缩与解压Zip(PHPZip类)

除非特别注明,鸡啄米文章均为原创
转载请标明本文地址:http://www.teaching4real.com/software/487.html
2015年12月23日
作者:鸡啄米 分类:软件开发 浏览: 注册送白菜网:0