php文件在线压缩程序类
提供一款超完美的php文件在线压缩程序,原理很简单就是把文件以二进制形式保存了,以前用过利用rar的内核程序,这是php自带的压缩功能,实例代码如下:
- set_time_limit(0);
- class phpzip{
-
- var $file_count = 0 ;
- var $datastr_len = 0;
- var $dirstr_len = 0;
- var $filedata = '';
- var $gzfilename;
- var $fp;
- var $dirstr='';
-
-
-
-
-
- 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 startfile($path = 'faisun.zip'){
- $this->gzfilename=$path;
- $mypathdir=array();
- do{
- $mypathdir[] = $path = dirname($path);
- }while($path != '.');
- @end($mypathdir);
- do{
- $path = @current($mypathdir);
- @mkdir($path);
- }while(@prev($mypathdir));
-
- if($this->fp=@fopen($this->gzfilename,"w")){
- return true;
- }
- return false;
- }
-
-
-
-
- function addfile($data, $name){
- $name = str_replace('\', '/', $name);
-
- if(strrchr($name,'/')=='/') return $this->adddir($name);
-
- $dtime = dechex($this->unix2dostime());
- $hexdtime = 'x' . $dtime[6] . $dtime[7]
- . 'x' . $dtime[4] . $dtime[5]
- . 'x' . $dtime[2] . $dtime[3]
- . 'x' . $dtime[0] . $dtime[1];
- eval('$hexdtime = "' . $hexdtime . '";');
-
- $unc_len = strlen($data);
- $crc = crc32($data);
- $zdata = gzcompress($data);
- $c_len = strlen($zdata);
- $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
-
-
- $datastr = "PK";
- $datastr .= "";
- $datastr .= "";
- $datastr .= "";
- $datastr .= $hexdtime;
- $datastr .= pack('v', $crc);
- $datastr .= pack('v', $c_len);
- $datastr .= pack('v', $unc_len);
- $datastr .= pack('v', strlen($name));
- $datastr .= pack('v', 0);
- $datastr .= $name;
- $datastr .= $zdata;
- $datastr .= pack('v', $crc);
- $datastr .= pack('v', $c_len);
- $datastr .= pack('v', $unc_len);
-
-
- fwrite($this->fp,$datastr);
- $my_datastr_len = strlen($datastr);
- unset($datastr);
-
-
- $dirstr = "PK";
- $dirstr .= "";
- $dirstr .= "";
- $dirstr .= "";
- $dirstr .= "";
- $dirstr .= $hexdtime;
- $dirstr .= pack('v', $crc);
- $dirstr .= pack('v', $c_len);
- $dirstr .= pack('v', $unc_len);
- $dirstr .= pack('v', strlen($name) );
- $dirstr .= pack('v', 0 );
- $dirstr .= pack('v', 0 );
- $dirstr .= pack('v', 0 );
- $dirstr .= pack('v', 0 );
- $dirstr .= pack('v', 32 );
- $dirstr .= pack('v',$this->datastr_len );
- $dirstr .= $name;
-
- $this->dirstr .= $dirstr;
-
- $this -> file_count ++;
- $this -> dirstr_len += strlen($dirstr);
- $this -> datastr_len += $my_datastr_len;
- }
-
- function adddir($name){
- $name = str_replace("\", "/", $name);
- $datastr = "PK ";
-
- $datastr .= pack("v",0).pack("v",0).pack("v",0).pack("v", strlen($name) );
- $datastr .= pack("v", 0 ).$name.pack("v", 0).pack("v", 0).pack("v", 0);
-
- fwrite($this->fp,$datastr);
- $my_datastr_len = strlen($datastr);
- unset($datastr);
-
- $dirstr = "PK ";
- $dirstr .= pack("v",0).pack("v",0).pack("v",0).pack("v", strlen($name) );
- $dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 );
- $dirstr .= pack("v", 16 ).pack("v",$this->datastr_len).$name;
-
- $this->dirstr .= $dirstr;
-
- $this -> file_count ++;
- $this -> dirstr_len += strlen($dirstr);
- $this -> datastr_len += $my_datastr_len;
- }
- function createfile(){
-
- $endstr = "PK" .
- pack('v', $this -> file_count) .
- pack('v', $this -> file_count) .
- pack('v', $this -> dirstr_len) .
- pack('v', $this -> datastr_len) .
- "";
-
- fwrite($this->fp,$this->dirstr.$endstr);
- fclose($this->fp);
- }
- }
|