日期:2014-05-17  浏览次数:20626 次

压缩与解压Zip(PHPZip类)

?

PHPZip类来源:网络

visitFile()函数来源:http://topic.csdn.net/u/20071225/11/e6a8db84-df02-4622-987b-2a4ed4a5eef0.html (第14楼)

注:修改为可遍历子文件夹(替换遍历方法为visitFile());

?? ? 添加GetZipComment()方法;

?? ? 加上用法说明(注释部分)。

<?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 == '.' || $file == '..'){ 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['year'] < 1980)
            {
                $timearray['year']    = 1980;
                $timearray['mon']     = 1;
                $timearray['mday']    = 1;
                $timearray['hours']   = 0;
                $timearray['minutes'] = 0;
                $timearray['seconds'] = 0;
            }
    
            return (  ($timearray['year'] - 1980) << 25)
                    | ($timearray['mon'] << 21)
                    | ($timearray['mday'] << 16)
                    | ($timearray['hours'] << 11)
                    | ($timearray['minutes'] << 5)
                    | ($timearray['seconds'] >> 1);
        }
        
        
        var $old_offset = 0;
        private function addFile($data, $filename, $time = 0)
        {
            $filename = str_replace('\\', '/', $filename);
    
            $dtime    = dechex($this->unix2DosTime($time));
            $hexdtime = '\x' . $dtime[6] . $dtime[7]
                      . '\x' . $dtime[4] . $dtime[5]
                      . '\x' . $dtime[2] . $dtime[3]
                      . '\x' . $dtime[0] . $dtime[1];
            eval('$hexdtime = "' . $hexdtime . '";');
    
            $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('V', $crc);
            $fr      .= pack('V', $c_len);
            $fr      .= pack('V', $unc_len);
            $fr      .= pack('v', strlen($filename));
            $fr      .= pack('v', 0);
            $fr      .= $filename;
    
            $fr      .= $zdata;
    
            $fr      .= pack('V', $crc);
            $fr      .= pack('V', $c_len);
            $fr      .= pack('V', $unc_len);
    
            $this->datasec[] = $fr;
            $new_offset      = strlen(implode('', $this->datasec));
    
            $cdrec  = "\x50\x4