>php教程 >PHP源码 >PHP压缩与解压Zip(PHPZip类)

PHP压缩与解压Zip(PHPZip类)

PHP中文网
PHP中文网원래의
2016-05-25 17:12:541028검색

PHP压缩与解压Zip,已经成功实现,可以放心使用

<?php
    class PHPZip
    {
        private $ctrl_dir     = array();
        private $datasec      = array();
 
 
        /**********************************************************
         * 压缩部分
         **********************************************************/
        // ------------------------------------------------------ //
        // #遍历指定文件夹
        //
        // $archive  = new PHPZip();
        // $filelist = $archive->visitFile(文件夹路径);
        // print "当前文件夹的文件:<p>\r\n";
        // foreach($filelist as $file)
        //     printf("%s<br>\r\n", $file);
        // ------------------------------------------------------ //
        var $fileList = array();
        public function visitFile($path)
        {
            global $fileList;
            $path = str_replace("\\", "/", $path);
            $fdir = dir($path);
         
            while(($file = $fdir->read()) !== false)
            {
                if($file == &#39;.&#39; || $file == &#39;..&#39;){ continue; }
         
                $pathSub    = preg_replace("*/{2,}*", "/", $path."/".$file);  // 替换多个反斜杠
                $fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub;
                if(is_dir($pathSub)){ $this->visitFile($pathSub); }
            }
            $fdir->close();
            return $fileList;
        }
         
         
        private function unix2DosTime($unixtime = 0)
        {
            $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
     
            if($timearray[&#39;year&#39;] < 1980)
            {
                $timearray[&#39;year&#39;]    = 1980;
                $timearray[&#39;mon&#39;]     = 1;
                $timearray[&#39;mday&#39;]    = 1;
                $timearray[&#39;hours&#39;]   = 0;
                $timearray[&#39;minutes&#39;] = 0;
                $timearray[&#39;seconds&#39;] = 0;
            }
     
            return (  ($timearray[&#39;year&#39;] - 1980) << 25)
                    | ($timearray[&#39;mon&#39;] << 21)
                    | ($timearray[&#39;mday&#39;] << 16)
                    | ($timearray[&#39;hours&#39;] << 11)
                    | ($timearray[&#39;minutes&#39;] << 5)
                    | ($timearray[&#39;seconds&#39;] >> 1);
        }
         
         
        var $old_offset = 0;
        private function addFile($data, $filename, $time = 0)
        {
            $filename = str_replace(&#39;\\&#39;, &#39;/&#39;, $filename);
     
            $dtime    = dechex($this->unix2DosTime($time));
            $hexdtime = &#39;\x&#39; . $dtime[6] . $dtime[7]
                      . &#39;\x&#39; . $dtime[4] . $dtime[5]
                      . &#39;\x&#39; . $dtime[2] . $dtime[3]
                      . &#39;\x&#39; . $dtime[0] . $dtime[1];
            eval(&#39;$hexdtime = "&#39; . $hexdtime . &#39;";&#39;);
     
            $fr       = "\x50\x4b\x03\x04";
            $fr      .= "\x14\x00";
            $fr      .= "\x00\x00";
            $fr      .= "\x08\x00";
            $fr      .= $hexdtime;
            $unc_len  = strlen($data);
            $crc      = crc32($data);
            $zdata    = gzcompress($data);
            $c_len    = strlen($zdata);
            $zdata    = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
            $fr      .= pack(&#39;V&#39;, $crc);
            $fr      .= pack(&#39;V&#39;, $c_len);
            $fr      .= pack(&#39;V&#39;, $unc_len);
            $fr      .= pack(&#39;v&#39;, strlen($filename));
            $fr      .= pack(&#39;v&#39;, 0);
            $fr      .= $filename;
     
            $fr      .= $zdata;
     
            $fr      .= pack(&#39;V&#39;, $crc);
            $fr      .= pack(&#39;V&#39;, $c_len);
            $fr      .= pack(&#39;V&#39;, $unc_len);
     
            $this->datasec[] = $fr;
            $new_offset      = strlen(implode(&#39;&#39;, $this->datasec));
     
            $cdrec  = "\x50\x4b\x01\x02";
            $cdrec .= "\x00\x00";
            $cdrec .= "\x14\x00";
            $cdrec .= "\x00\x00";
            $cdrec .= "\x08\x00";
            $cdrec .= $hexdtime;
            $cdrec .= pack(&#39;V&#39;, $crc);
            $cdrec .= pack(&#39;V&#39;, $c_len);
            $cdrec .= pack(&#39;V&#39;, $unc_len);
            $cdrec .= pack(&#39;v&#39;, strlen($filename) );
            $cdrec .= pack(&#39;v&#39;, 0 );
            $cdrec .= pack(&#39;v&#39;, 0 );
            $cdrec .= pack(&#39;v&#39;, 0 );
            $cdrec .= pack(&#39;v&#39;, 0 );
            $cdrec .= pack(&#39;V&#39;, 32 );
     
            $cdrec .= pack(&#39;V&#39;, $this->old_offset );
            $this->old_offset = $new_offset;
     
            $cdrec .= $filename;
            $this->ctrl_dir[] = $cdrec;
        }
         
         
        var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
        private function file()
        {
            $data    = implode(&#39;&#39;, $this->datasec);
            $ctrldir = implode(&#39;&#39;, $this->ctrl_dir);
     
            return   $data
                   . $ctrldir
                   . $this->eof_ctrl_dir
                   . pack(&#39;v&#39;, sizeof($this->ctrl_dir))
                   . pack(&#39;v&#39;, sizeof($this->ctrl_dir))
                   . pack(&#39;V&#39;, strlen($ctrldir))
                   . pack(&#39;V&#39;, strlen($data))
                   . "\x00\x00";
        }
     
         
        // ------------------------------------------------------ //
        // #压缩到服务器
        //
        // $archive = new PHPZip();
        // $archive->Zip("需压缩的文件所在目录", "ZIP压缩文件名"); 
        // ------------------------------------------------------ //
        public function Zip($dir, $saveName)
        {
            if(@!function_exists(&#39;gzcompress&#39;)){ return; }
     
            ob_end_clean();
            $filelist = $this->visitFile($dir);
            if(count($filelist) == 0){ return; }
     
            foreach($filelist as $file)
            {
                if(!file_exists($file) || !is_file($file)){ continue; }
                 
                $fd       = fopen($file, "rb");
                $content  = @fread($fd, filesize($file));
                fclose($fd);
 
                // 1.删除$dir的字符(./folder/file.txt删除./folder/)
                // 2.如果存在/就删除(/file.txt删除/)
                $file = substr($file, strlen($dir));
                if(substr($file, 0, 1) == "\\" || substr($file, 0, 1) == "/"){ $file = substr($file, 1); }
                 
                $this->addFile($content, $file);
            }
            $out = $this->file();
     
            $fp = fopen($saveName, "wb");
            fwrite($fp, $out, strlen($out));
            fclose($fp);
        }
     
     
        // ------------------------------------------------------ //
        // #压缩并直接下载
        //
        // $archive = new PHPZip();
        // $archive->ZipAndDownload("需压缩的文件所在目录");
        // ------------------------------------------------------ //
        public function ZipAndDownload($dir)
        {
            if(@!function_exists(&#39;gzcompress&#39;)){ return; }
     
            ob_end_clean();
            $filelist = $this->visitFile($dir);
            if(count($filelist) == 0){ return; }
     
            foreach($filelist as $file)
            {
                if(!file_exists($file) || !is_file($file)){ continue; }
                 
                $fd       = fopen($file, "rb");
                $content  = @fread($fd, filesize($file));
                fclose($fd);
     
                // 1.删除$dir的字符(./folder/file.txt删除./folder/)
                // 2.如果存在/就删除(/file.txt删除/)
                $file = substr($file, strlen($dir));
                if(substr($file, 0, 1) == "\\" || substr($file, 0, 1) == "/"){ $file = substr($file, 1); }
                 
                $this->addFile($content, $file);
            }
            $out = $this->file();
     
            @header(&#39;Content-Encoding: none&#39;);
            @header(&#39;Content-Type: application/zip&#39;);
            @header(&#39;Content-Disposition: attachment ; filename=Farticle&#39;.date("YmdHis", time()).&#39;.zip&#39;);
            @header(&#39;Pragma: no-cache&#39;);
            @header(&#39;Expires: 0&#39;);
            print($out);
        }
         
         
         
         
         
        /**********************************************************
         * 解压部分
         **********************************************************/
        // ------------------------------------------------------ //
        // ReadCentralDir($zip, $zipfile)
        // $zip是经过@fopen($zipfile, &#39;rb&#39;)打开的
        // $zipfile是zip文件的路径
        // ------------------------------------------------------ //
        private function ReadCentralDir($zip, $zipfile)
        {
            $size     = filesize($zipfile);
            $max_size = ($size < 277) ? $size : 277;
             
            @fseek($zip, $size - $max_size);
            $pos   = ftell($zip);
            $bytes = 0x00000000;
             
            while($pos < $size)
            {
                $byte  = @fread($zip, 1);
                $bytes = ($bytes << 8) | Ord($byte);
                $pos++;
                if($bytes == 0x504b0506){ break; }
            }
             
            $data = unpack(&#39;vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size&#39;, fread($zip, 18));
 
            $centd[&#39;comment&#39;]      = ($data[&#39;comment_size&#39;] != 0) ? fread($zip, $data[&#39;comment_size&#39;]) : &#39;&#39;;  // 注释
            $centd[&#39;entries&#39;]      = $data[&#39;entries&#39;];
            $centd[&#39;disk_entries&#39;] = $data[&#39;disk_entries&#39;];
            $centd[&#39;offset&#39;]       = $data[&#39;offset&#39;];
            $centd[&#39;disk_start&#39;]   = $data[&#39;disk_start&#39;];
            $centd[&#39;size&#39;]         = $data[&#39;size&#39;];
            $centd[&#39;disk&#39;]         = $data[&#39;disk&#39;];
            return $centd;
        }
         
         
        private function ReadCentralFileHeaders($zip)
        {
            $binary_data = fread($zip, 46);
            $header      = unpack(&#39;vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset&#39;, $binary_data);
 
            $header[&#39;filename&#39;] = ($header[&#39;filename_len&#39;] != 0) ? fread($zip, $header[&#39;filename_len&#39;]) : &#39;&#39;;
            $header[&#39;extra&#39;]    = ($header[&#39;extra_len&#39;]    != 0) ? fread($zip, $header[&#39;extra_len&#39;])    : &#39;&#39;;
            $header[&#39;comment&#39;]  = ($header[&#39;comment_len&#39;]  != 0) ? fread($zip, $header[&#39;comment_len&#39;])  : &#39;&#39;;
 
     
            if($header[&#39;mdate&#39;] && $header[&#39;mtime&#39;])
            {
                $hour    = ($header[&#39;mtime&#39;]  & 0xF800) >> 11;
                $minute  = ($header[&#39;mtime&#39;]  & 0x07E0) >> 5;
                $seconde = ($header[&#39;mtime&#39;]  & 0x001F) * 2;
                $year    = (($header[&#39;mdate&#39;] & 0xFE00) >> 9) + 1980;
                $month   = ($header[&#39;mdate&#39;]  & 0x01E0) >> 5;
                $day     = $header[&#39;mdate&#39;]   & 0x001F;
                $header[&#39;mtime&#39;] = mktime($hour, $minute, $seconde, $month, $day, $year);
            } else {
                $header[&#39;mtime&#39;] = time();
            }
            $header[&#39;stored_filename&#39;] = $header[&#39;filename&#39;];
            $header[&#39;status&#39;] = &#39;ok&#39;;
            if(substr($header[&#39;filename&#39;], -1) == &#39;/&#39;){ $header[&#39;external&#39;] = 0x41FF0010; }  // 判断是否文件夹
            return $header;
        }
     
     
        private function ReadFileHeader($zip)
        {
            $binary_data = fread($zip, 30);
            $data        = unpack(&#39;vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len&#39;, $binary_data);
     
            $header[&#39;filename&#39;]        = fread($zip, $data[&#39;filename_len&#39;]);
            $header[&#39;extra&#39;]           = ($data[&#39;extra_len&#39;] != 0) ? fread($zip, $data[&#39;extra_len&#39;]) : &#39;&#39;;
            $header[&#39;compression&#39;]     = $data[&#39;compression&#39;];
            $header[&#39;size&#39;]            = $data[&#39;size&#39;];
            $header[&#39;compressed_size&#39;] = $data[&#39;compressed_size&#39;];
            $header[&#39;crc&#39;]             = $data[&#39;crc&#39;];
            $header[&#39;flag&#39;]            = $data[&#39;flag&#39;];
            $header[&#39;mdate&#39;]           = $data[&#39;mdate&#39;];
            $header[&#39;mtime&#39;]           = $data[&#39;mtime&#39;];
     
            if($header[&#39;mdate&#39;] && $header[&#39;mtime&#39;]){
                $hour    = ($header[&#39;mtime&#39;]  & 0xF800) >> 11;
                $minute  = ($header[&#39;mtime&#39;]  & 0x07E0) >> 5;
                $seconde = ($header[&#39;mtime&#39;]  & 0x001F) * 2;
                $year    = (($header[&#39;mdate&#39;] & 0xFE00) >> 9) + 1980;
                $month   = ($header[&#39;mdate&#39;]  & 0x01E0) >> 5;
                $day     = $header[&#39;mdate&#39;]   & 0x001F;
                $header[&#39;mtime&#39;] = mktime($hour, $minute, $seconde, $month, $day, $year);
            }else{
                $header[&#39;mtime&#39;] = time();
            }
     
            $header[&#39;stored_filename&#39;] = $header[&#39;filename&#39;];
            $header[&#39;status&#39;]          = "ok";
            return $header;
        }
     
     
        private function ExtractFile($header, $to, $zip)
        {
            $header = $this->readfileheader($zip);
             
            if(substr($to, -1) != "/"){ $to .= "/"; }
            if(!@is_dir($to)){ @mkdir($to, 0777); }
             
            $pth = explode("/", dirname($header[&#39;filename&#39;]));
            for($i=0; isset($pth[$i]); $i++){
                if(!$pth[$i]){ continue; }
                $pthss .= $pth[$i]."/";
                if(!is_dir($to.$pthss)){ @mkdir($to.$pthss, 0777); }
            }
             
            if(!($header[&#39;external&#39;] == 0x41FF0010) && !($header[&#39;external&#39;] == 16))
            {
                if($header[&#39;compression&#39;] == 0)
                {
                    $fp = @fopen($to.$header[&#39;filename&#39;], &#39;wb&#39;);
                    if(!$fp){ return(-1); }
                    $size = $header[&#39;compressed_size&#39;];
                     
                    while($size != 0)
                    {
                        $read_size   = ($size < 2048 ? $size : 2048);
                        $buffer      = fread($zip, $read_size);
                        $binary_data = pack(&#39;a&#39;.$read_size, $buffer);
                        @fwrite($fp, $binary_data, $read_size);
                        $size       -= $read_size;
                    }
                    fclose($fp);
                    touch($to.$header[&#39;filename&#39;], $header[&#39;mtime&#39;]);
                 
                }else{
                     
                    $fp = @fopen($to.$header[&#39;filename&#39;].&#39;.gz&#39;, &#39;wb&#39;);
                    if(!$fp){ return(-1); }
                    $binary_data = pack(&#39;va1a1Va1a1&#39;, 0x8b1f, Chr($header[&#39;compression&#39;]), Chr(0x00), time(), Chr(0x00), Chr(3));
                     
                    fwrite($fp, $binary_data, 10);
                    $size = $header[&#39;compressed_size&#39;];
                     
                    while($size != 0)
                    {
                        $read_size   = ($size < 1024 ? $size : 1024);
                        $buffer      = fread($zip, $read_size);
                        $binary_data = pack(&#39;a&#39;.$read_size, $buffer);
                        @fwrite($fp, $binary_data, $read_size);
                        $size       -= $read_size;
                    }
                     
                    $binary_data = pack(&#39;VV&#39;, $header[&#39;crc&#39;], $header[&#39;size&#39;]);
                    fwrite($fp, $binary_data, 8);
                    fclose($fp);
                     
                    $gzp = @gzopen($to.$header[&#39;filename&#39;].&#39;.gz&#39;, &#39;rb&#39;) or die("Cette archive est compress!");
                     
                    if(!$gzp){ return(-2); }
                    $fp = @fopen($to.$header[&#39;filename&#39;], &#39;wb&#39;);
                    if(!$fp){ return(-1); }
                    $size = $header[&#39;size&#39;];
                     
                    while($size != 0)
                    {
                        $read_size   = ($size < 2048 ? $size : 2048);
                        $buffer      = gzread($gzp, $read_size);
                        $binary_data = pack(&#39;a&#39;.$read_size, $buffer);
                        @fwrite($fp, $binary_data, $read_size);
                        $size       -= $read_size;
                    }
                    fclose($fp); gzclose($gzp);
                     
                    touch($to.$header[&#39;filename&#39;], $header[&#39;mtime&#39;]);
                    @unlink($to.$header[&#39;filename&#39;].&#39;.gz&#39;);
                }
            }
            return true;
        }
         
         
        // ------------------------------------------------------ //
        // #解压文件
        //
        // $archive   = new PHPZip();
        // $zipfile   = "ZIP压缩文件名";
        // $savepath  = "解压缩目录名";
        // $zipfile   = $unzipfile;
        // $savepath  = $unziptarget;
        // $array     = $archive->GetZipInnerFilesInfo($zipfile);
        // $filecount = 0;
        // $dircount  = 0;
        // $failfiles = array();
        // set_time_limit(0);  // 修改为不限制超时时间(默认为30秒)
        //
        // for($i=0; $i<count($array); $i++) {
        //     if($array[$i][folder] == 0){
        //         if($archive->unZip($zipfile, $savepath, $i) > 0){
        //             $filecount++;
        //         }else{
        //             $failfiles[] = $array[$i][filename];
        //         }
        //     }else{
        //         $dircount++;
        //     }
        // }
        // set_time_limit(30);
        //printf("文件夹:%d    解压文件:%d    失败:%d<br>\r\n", $dircount, $filecount, count($failfiles));
        //if(count($failfiles) > 0){
        //    foreach($failfiles as $file){
        //        printf("&middot;%s<br>\r\n", $file);
        //    }
        //}
        // ------------------------------------------------------ //
        public function unZip($zipfile, $to, $index = Array(-1))
        {
            $ok  = 0;
            $zip = @fopen($zipfile, &#39;rb&#39;);
            if(!$zip){ return(-1); }
             
            $cdir      = $this->ReadCentralDir($zip, $zipfile);
            $pos_entry = $cdir[&#39;offset&#39;];
             
            if(!is_array($index)){ $index = array($index); }
            for($i=0; $index[$i]; $i++)
            {
                if(intval($index[$i]) != $index[$i] || $index[$i] > $cdir[&#39;entries&#39;])
                {
                    return(-1);
                }
            }
             
            for($i=0; $i<$cdir[&#39;entries&#39;]; $i++)
            {
                @fseek($zip, $pos_entry);
                $header          = $this->ReadCentralFileHeaders($zip);
                $header[&#39;index&#39;] = $i;
                $pos_entry       = ftell($zip);
                @rewind($zip);
                fseek($zip, $header[&#39;offset&#39;]);
                if(in_array("-1", $index) || in_array($i, $index))
                {
                    $stat[$header[&#39;filename&#39;]] = $this->ExtractFile($header, $to, $zip);
                }
            }
             
            fclose($zip);
            return $stat;
        }
         
         
         
         
         
        /**********************************************************
         * 其它部分
         **********************************************************/
        // ------------------------------------------------------ //
        // #获取被压缩文件的信息
        //
        // $archive = new PHPZip();
        // $array = $archive->GetZipInnerFilesInfo(ZIP压缩文件名);
        // for($i=0; $i<count($array); $i++) {
        //     printf("<b>&middot;%s</b><br>\r\n", $array[$i][filename]);
        //     foreach($array[$i] as $key => $value)
        //         printf("%s => %s<br>\r\n", $key, $value);
        //     print "\r\n<p>------------------------------------<p>\r\n\r\n";
        // }
        // ------------------------------------------------------ //
        public function GetZipInnerFilesInfo($zipfile)
        {
            $zip = @fopen($zipfile, &#39;rb&#39;);
            if(!$zip){ return(0); }
            $centd = $this->ReadCentralDir($zip, $zipfile);
             
            @rewind($zip);
            @fseek($zip, $centd[&#39;offset&#39;]);
            $ret = array();
 
            for($i=0; $i<$centd[&#39;entries&#39;]; $i++)
            {
                $header          = $this->ReadCentralFileHeaders($zip);
                $header[&#39;index&#39;] = $i;
                $info = array(
                    &#39;filename&#39;        => $header[&#39;filename&#39;],                   // 文件名
                    &#39;stored_filename&#39; => $header[&#39;stored_filename&#39;],            // 压缩后文件名
                    &#39;size&#39;            => $header[&#39;size&#39;],                       // 大小
                    &#39;compressed_size&#39; => $header[&#39;compressed_size&#39;],            // 压缩后大小
                    &#39;crc&#39;             => strtoupper(dechex($header[&#39;crc&#39;])),    // CRC32
                    &#39;mtime&#39;           => date("Y-m-d H:i:s",$header[&#39;mtime&#39;]),  // 文件修改时间
                    &#39;comment&#39;         => $header[&#39;comment&#39;],                    // 注释
                    &#39;folder&#39;          => ($header[&#39;external&#39;] == 0x41FF0010 || $header[&#39;external&#39;] == 16) ? 1 : 0,  // 是否为文件夹
                    &#39;index&#39;           => $header[&#39;index&#39;],                      // 文件索引
                    &#39;status&#39;          => $header[&#39;status&#39;]                      // 状态
                );
                $ret[] = $info;
                unset($header);
            }
            fclose($zip);
            return $ret;
        }
         
         
        // ------------------------------------------------------ //
        // #获取压缩文件的注释
        //
        // $archive = new PHPZip();
        // echo $archive->GetZipComment(ZIP压缩文件名);
        // ------------------------------------------------------ //
        public function GetZipComment($zipfile)
        {
            $zip = @fopen($zipfile, &#39;rb&#39;);
            if(!$zip){ return(0); }
            $centd = $this->ReadCentralDir($zip, $zipfile);
            fclose($zip);
            return $centd[comment];
        }
    }
?>
 
 
 
 
 
<form method="post">
  压缩到服务器:
  <br>------------------<br>
  压缩目录:     <input name="zipdir" type="text" id="zipdir" />(例如./folder)<br>
  另存为路径和文件名:<input name="savename" type="text" id="savename" />(例如./folder/file.zip)(需要填写扩展名)<br>
  <input name="zip" type="submit" id="zip" value="压缩" /><br>
  <br><br><br>
   
  压缩并下载
  <br>------------------<br>
  文件所在目录:<input name="zipdowndir" type="text" id="zipdowndir" />(例如./folder)<br>
  <input name="zipdown" type="submit" id="zipdown" value="压缩并下载" />
  <br><br><br>
   
  在线解压zip
  <br>------------------<br>
  文件: <input name="unzipfile" type="text" id="unzipfile" />(例如./folder/file.zip)<br>
  解压到:<input name="unziptarget" type="text" id="unziptarget" />(例如./folder)<br>
  <input name="unzip" type="submit" id="unzip" value="解压" />
  <br><br><br>
   
  读取压缩文件内部文件信息和注释
  <br>------------------<br>
  文件:<input name="readfile" type="text" id="readfile" />(例如./folder/file.zip)<br>
  <input name="readfileinfo" type="submit" id="readfileinfo" value="读取内部文件信息" />
  <input name="readcomment" type="submit" id="readcomment" value="读取注释" />
</form>
<?
    echo "<p><p>\r\n";
    echo "<br>--------------------------<br>\r\n";
    echo "显示信息:\r\n";
    echo "<br>--------------------------<br>\r\n";
     
    $archive  = new PHPZip();
    if(!empty($zip))
    {
        $archive->Zip($zipdir, $savename);
    }
    elseif(!empty($zipdown))
    {
        $archive->ZipAndDownload($zipdowndir);
    }
    elseif(!empty($unzip))
    {
        $zipfile   = $unzipfile;
        $savepath  = $unziptarget;
        $array     = $archive->GetZipInnerFilesInfo($zipfile);
        $filecount = 0;
        $dircount  = 0;
        $failfiles = array();
        set_time_limit(0);  // 修改为不限制超时时间(默认为30秒)
         
        for($i=0; $i<count($array); $i++) {
            if($array[$i][folder] == 0){
                if($archive->unZip($zipfile, $savepath, $i) > 0){
                    $filecount++;
                }else{
                    $failfiles[] = $array[$i][filename];
                }
            }else{
                $dircount++;
            }
        }
 
             set_time_limit(30);
 
printf("文件夹:%d    解压文件:%d    失败:%d<br>\r\n", $dircount, $filecount, count($failfiles));
        if(count($failfiles) > 0){
            foreach($failfiles as $file){
                printf("&middot;%s<br>\r\n", $file);
            }
        }
    }
    elseif(!empty($readfileinfo))
    {
        $array = $archive->GetZipInnerFilesInfo($readfile);
        for($i=0; $i<count($array); $i++) {
            printf("<b>&middot;%s</b><br>\r\n", $array[$i][filename]);
            foreach($array[$i] as $key => $value)
                printf("%s => %s<br>\r\n", $key, $value);
            print "\r\n<p>------------------------------------<p>\r\n\r\n";
        }
    }
    elseif(!empty($readcomment))
    {
        $comment = $archive->GetZipComment($readfile);
        printf("%s<br>\r\n", $comment);
    }
?>

02111113_Dbps.png

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.