Home > php教程 > php手册 > body text

php实现文件在线压缩功能

WBOY
Release: 2016-06-13 11:21:56
Original
920 people have browsed it



class ZipAllFloder
{
    var 
$cachelist 
= array();
    var 
$datasec      
= array();  
    var 
$ctrl_dir     
= array();  
    var 
$eof_ctrl_dir "x50x4bx05x06x00x00x00x00"
;  
    var 
$old_offset   0
;  
    
    function 
StartWork($dir
){
        
$result $this->GetList($dir
);
        
$this->ClearCache
();
        return 
$result
;
    } 
    function 
ReadAndExport($filelist,$zipfilename
){  
        if (
count($filelist)>0
){  
            foreach(
$filelist as $filename
){  
                if (
is_file($filename
)){  
                    
$fp fopen ($filename"r"
);  
                    
$content = @fread ($fp,filesize($filename
));  
                    
fclose ($fp
);  
                    
$filename basename($filename
);  
                    
$this -> addFile($content$filename
);  
                }  
            }  
        }else{
            
$this -> addFile("This folder have none a single file!","nofile.log"
);
        }
        
$out $this -> outfile
(); 
        
$this -> IniVars
();
        
$fp fopen($zipfilename,"w"
);  
        
fwrite($fp,$out,strlen($out
));  
        
fclose($fp
);
        
$this -> cachelist[] = $zipfilename

    }
    function 
GetList($dir
){  
        if (
file_exists($dir
)){  
            
$handle opendir($dir
); 
            while(
$files readdir($handle
)){  
                if ((
$files==".")||($files==".."
)) continue; 
                if(
is_dir($dir."/".$files
)){
                    
$this -> GetList($dir."/".$files
);
                }else{
                    
$filelist[] = $dir."/".$files

                } 
            }  
            
closedir($handle
);
            
$zipfilename dirname($dir)."/[FOLDER]".basename($dir).".zip"
;
            
$this -> ReadAndExport($filelist,$zipfilename
);
        }  
        return 
$zipfilename
;  
    }  
    function 
ClearCache
(){
        if(
$num count($this ->cachelist
)){
            for(
$i=0;$i$num-1;$i
++){
                @
unlink($this ->cachelist[$i
]);
            }
        }
    }
    function 
IniVars
(){
        
$this -> datasec 
= array();
        
$this -> ctrl_dir 
= array();
        
$this -> eof_ctrl_dir "x50x4bx05x06x00x00x00x00"
;
        
$this -> old_offset   0
;
    }
    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
);  
    }
    function 
addFile($data$name$time 0
){  
        
$name str_replace('\''/'$name
);  
        
$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   "x50x4bx03x04"
;  
        
$fr   .= "x14x00"
;
        
$fr   .= "x00x00"
;
        
$fr   .= "x08x00"
;
        
$fr   .= $hexdtime

        
$unc_len strlen($data
);  
        
$crc     crc32($data
);  
        
$zdata   gzcompress($data
);  
        
$c_len   strlen($zdata
);  
        
$zdata   substr(substr($zdata0strlen($zdata) - 4), 2
);
        
$fr      .= pack('V'$crc
);
        
$fr      .= pack('V'$c_len
);
        
$fr      .= pack('V'$unc_len
);
        
$fr      .= pack('v'strlen($name
));
        
$fr      .= pack('v'0
);
        
$fr      .= $name
;  
        
$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 "x50x4bx01x02"
;  
        
$cdrec .= "x00x00"
;
        
$cdrec .= "x14x00"
;
        
$cdrec .= "x00x00"
;
        
$cdrec .= "x08x00"
;
        
$cdrec .= $hexdtime
;
        
$cdrec .= pack('V'$crc
);
        
$cdrec .= pack('V'$c_len
);
        
$cdrec .= pack('V'$unc_len
);
        
$cdrec .= pack('v'strlen($name
) );
        
$cdrec .= pack('v'
);
        
$cdrec .= pack('v'
);
        
$cdrec .= pack('v'
);
        
$cdrec .= pack('v'
);
        
$cdrec .= pack('V'32 
);
        
$cdrec .= pack('V'$this -> old_offset 
);
        
$this -> old_offset $new_offset
;  
        
$cdrec .= $name
;  
        
$this -> ctrl_dir[] = $cdrec
;  
    }
    function 
outfile
(){  
        
$data    implode(''$this -> datasec
);  
        
$ctrldir implode(''$this -> ctrl_dir
);  
        return  
            
$data 
.  
            
$ctrldir 
.  
            
$this -> eof_ctrl_dir 
.  
            
pack('v'sizeof($this -> ctrl_dir
)) .
            
pack('v'sizeof($this -> ctrl_dir
)) .
            
pack('V'strlen($ctrldir
)) .
            
pack('V'strlen($data
)) .
            
"x00x00"
;
    }
}

$zip = new ZipAllFloder
();
$str $zip->StartWork("./baby"
);
echo 
$str
;
?>


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template