//以下是类及函数定义
class Zip //ZIP压缩类
{
var $datasec, $ctrl_dir = array();
var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
var $old_offset = 0; var $dirs = Array(".");
function get_List($zip_name)
{
$zip = @fopen($zip_name, 'rb');
if(!$zip) return(0);
$centd = $this->ReadCentralDir($zip,$zip_name);
@rewind($zip);
@fseek($zip, $centd['offset']);
for ($i=0; $i<$centd[&#39;entries&#39;]; $i++)
{
$header = $this->ReadCentralFileHeaders($zip);
$header[&#39;index&#39;] = $i;$info[&#39;filename&#39;] = $header[&#39;filename&#39;];
$info[&#39;stored_filename&#39;] = $header[&#39;stored_filename&#39;];
$info[&#39;size&#39;] = $header[&#39;size&#39;];$info[&#39;compressed_size&#39;]=$header[&#39;compressed_size&#39;];
$info[&#39;crc&#39;] = strtoupper(dechex( $header[&#39;crc&#39;] ));
$info[&#39;mtime&#39;] = $header[&#39;mtime&#39;]; $info[&#39;comment&#39;] = $header[&#39;comment&#39;];
$info[&#39;folder&#39;] = ($header[&#39;external&#39;]==0x41FF0010||$header[&#39;external&#39;]==16)?1:0;
$info[&#39;index&#39;] = $header[&#39;index&#39;];$info[&#39;status&#39;] = $header[&#39;status&#39;];
$ret[]=$info; unset($header);
}
return $ret;
}
function Add($files,$compact)
{
if(!is_array($files[0])) $files=Array($files);
for($i=0;$files[$i];$i++){
$fn = $files[$i];
if(!in_Array(dirname($fn[0]),$this->dirs))
$this->add_Dir(dirname($fn[0]));
if(basename($fn[0]))
$ret[basename($fn[0])]=$this->add_File($fn[1],$fn[0],$compact);
}
return $ret;
}
function get_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";
}
function add_dir($name)
{
$name = str_replace("\\", "/", $name);
$fr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00";
$fr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) );
$fr .= pack("v", 0 ).$name.pack("V", 0).pack("V", 0).pack("V", 0);
$this -> datasec[] = $fr;
$new_offset = strlen(implode("", $this->datasec));
$cdrec = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00";
$cdrec .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) );
$cdrec .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 );
$ext = "\xff\xff\xff\xff";
$cdrec .= pack("V", 16 ).pack("V", $this -> old_offset ).$name;
$this -> ctrl_dir[] = $cdrec;
$this -> old_offset = $new_offset;
$this -> dirs[] = $name;
}
function add_File($data, $name, $compact = 1)
{
$name = str_replace(&#39;\\&#39;, &#39;/&#39;, $name);
$dtime = dechex($this->DosTime());
$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;);
if($compact)
$fr = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00".$hexdtime;
else $fr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00".$hexdtime;
$unc_len = strlen($data); $crc = crc32($data);
if($compact){
$zdata = gzcompress($data); $c_len = strlen($zdata);
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
}else{
$zdata = $data;
}
$c_len=strlen($zdata);
$fr .= pack(&#39;V&#39;, $crc).pack(&#39;V&#39;, $c_len).pack(&#39;V&#39;, $unc_len);
$fr .= pack(&#39;v&#39;, strlen($name)).pack(&#39;v&#39;, 0).$name.$zdata;
$fr .= pack(&#39;V&#39;, $crc).pack(&#39;V&#39;, $c_len).pack(&#39;V&#39;, $unc_len);
$this -> datasec[] = $fr;
$new_offset = strlen(implode(&#39;&#39;, $this->datasec));
if($compact)
$cdrec = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00";
else $cdrec = "\x50\x4b\x01\x02\x14\x00\x0a\x00\x00\x00\x00\x00";
$cdrec .= $hexdtime.pack(&#39;V&#39;, $crc).pack(&#39;V&#39;, $c_len).pack(&#39;V&#39;, $unc_len);
$cdrec .= pack(&#39;v&#39;, strlen($name) ).pack(&#39;v&#39;, 0 ).pack(&#39;v&#39;, 0 );
$cdrec .= pack(&#39;v&#39;, 0 ).pack(&#39;v&#39;, 0 ).pack(&#39;V&#39;, 32 );
$cdrec .= pack(&#39;V&#39;, $this -> old_offset );
$this -> old_offset = $new_offset;
$cdrec .= $name;
$this -> ctrl_dir[] = $cdrec;
return true;
}
function DosTime() {
$timearray = getdate();
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);
}
function Extract ( $zn, $to, $index = Array(-1) )
{
$ok = 0; $zip = @fopen($zn,&#39;rb&#39;);
if(!$zip) return(-1);
$cdir = $this->ReadCentralDir($zip,$zn);
$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;
}
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;]);
if ($data[&#39;extra_len&#39;] != 0) {
$header[&#39;extra&#39;] = fread($zip, $data[&#39;extra_len&#39;]);
} else { $header[&#39;extra&#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;
}
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);
if ($header[&#39;filename_len&#39;] != 0)
$header[&#39;filename&#39;] = fread($zip,$header[&#39;filename_len&#39;]);
else $header[&#39;filename&#39;] = &#39;&#39;;
if ($header[&#39;extra_len&#39;] != 0)
$header[&#39;extra&#39;] = fread($zip, $header[&#39;extra_len&#39;]);
else $header[&#39;extra&#39;] = &#39;&#39;;
if ($header[&#39;comment_len&#39;] != 0)
$header[&#39;comment&#39;] = fread($zip, $header[&#39;comment_len&#39;]);
else $header[&#39;comment&#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;
}
function ReadCentralDir($zip,$zip_name)
{
$size = filesize($zip_name);
if ($size <277) $maximum_size = $size;
else $maximum_size=277;
@fseek($zip, $size-$maximum_size);
$pos = ftell($zip); $bytes = 0x00000000;
while ($pos <$size)
{
$byte = @fread($zip, 1); $bytes=($bytes <<8) | Ord($byte);
if ($bytes == 0x504b0506){ $pos++; break; } $pos++;
}
$data=unpack(&#39;vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size&#39;,fread($zip,18));
if ($data[&#39;comment_size&#39;] != 0)
$centd[&#39;comment&#39;] = fread($zip, $data[&#39;comment_size&#39;]);
else $centd[&#39;comment&#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;
}
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;
}
} //ZIP压缩类end