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 .= ""; // ver needed to extract
$datastr .= ""; // gen purpose bit flag
$datastr .= ""; // compression method
$datastr .= $hexdtime; // last mod time and date
$datastr .= pack('v', $crc); // crc32
$datastr .= pack('v', $c_len); // compressed filesize
$datastr .= pack('v', $unc_len); // uncompressed filesize
$datastr .= pack('v', strlen($name)); // length of filename
$datastr .= pack('v', 0); // extra field length
$datastr .= $name;
$datastr .= $zdata;
$datastr .= pack('v', $crc); // crc32
$datastr .= pack('v', $c_len); // compressed filesize
$datastr .= pack('v', $unc_len); // uncompressed filesize
fwrite($this->fp,$datastr); //写入新的文件内容
$my_datastr_len = strlen($datastr);
unset($datastr);
//新添文件目录信息
$dirstr = "PK";
$dirstr .= ""; // version made by
$dirstr .= ""; // version needed to extract
$dirstr .= ""; // gen purpose bit flag
$dirstr .= ""; // compression method
$dirstr .= $hexdtime; // last mod time & date
$dirstr .= pack('v', $crc); // crc32
$dirstr .= pack('v', $c_len); // compressed filesize
$dirstr .= pack('v', $unc_len); // uncompressed filesize
$dirstr .= pack('v', strlen($name) ); // length of filename
$dirstr .= pack('v', 0 ); // extra field length
$dirstr .= pack('v', 0 ); // file comment length
$dirstr .= pack('v', 0 ); // disk number start
$dirstr .= pack('v', 0 ); // internal file attributes
$dirstr .= pack('v', 32 ); // external file attributes - 'archive' bit set
$dirstr .= pack('v',$this->datastr_len ); // relative offset of local header
$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);
}
}
|