php解压缩文件方法汇总
一使用内部扩展实现(>=php5)function unzip_file($file, $destination){// create object
$zip = new ZipArchive() ;
// open archive
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
// extract contents to destination directory
$zip->extractTo($destination);
// close archive
$zip->close();
echo 'Archive extracted to directory';
}
unzip_file('./a.zip','bs');
二 不使用组件 完全使用php内部函数解压代码:
public function ExtractFile($header,$to,$zip) {
$header = $this->readfileheader($zip);
if (substr($to,-1)!="/") $to.="/";
if ($to=='./') $to = '';
$pth = explode("/",$to.$header['filename']);
$mydir = '';
for($i=0;$i<count($pth)-1;$i++) {
if (!$pth[$i]) continue;
$mydir .= $pth[$i]."/";
if ((!is_dir($mydir) && @mkdir($mydir,0777)) || (($mydir==$to.$header['filename'] || ($mydir==$to && $this->total_folders==0)) && is_dir($mydir)) ) {
@chmod($mydir,0777);
$this->total_folders ++;
echo 'Extract : ',$mydir,'<br>';
}
}
if (strrchr($header['filename'],'/')=='/') return;
if (!($header['external']==0x41FF0010)&&!($header['external']==16)) {
if ($header['compression']==0) {
$fp = @fopen($to.$header['filename'], 'wb');
if (!$fp) return(-1);
$size = $header['compressed_size'];
while ($size != 0) {
$read_size = ($size < 2048 ? $size : 2048);
$buffer = fread($zip, $read_size);
$binary_data = pack('a'.$read_size, $buffer);
@fwrite($fp, $binary_data, $read_size);
$size -= $read_size;
}
fclose($fp);
touch($to.$header['filename'], $header['mtime']);
} else {
$fp = @fopen($to.$header['filename'].'.gz','wb');
if (!$fp) return(-1);
$binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']),
Chr(0x00), time(), Chr(0x00), Chr(3));
fwrite($fp, $binary_data, 10);
$size = $header['compressed_size'];
while ($size != 0) {
$read_size = ($size < 1024 ? $size : 1024);
$buffer = fread($zip, $read_size);
$binary_data = pack('a'.$read_size, $buffer);
@fwrite($fp, $binary_data, $read_size);
$size -= $read_size;
}
$binary_data = pack('VV', $header['crc'], $header['size']);
fwrite($fp, $binary_data,8); fclose($fp);
$gzp = @gzopen($to.$header['filename'].'.gz','rb') or die("Cette archive est compress")
三 执行外部命令 ① exec("tar -zxvf xxx.tar.gz"); ② system("tar -zxvf xxx.tar.gz") ③ $obj=new com("wscript.shell");//加载组件 获得解压
$obj->run("winrar x $dir".$name." ".$dir ,1,true);//上传解压文件 // unlink($dir.$name);//删除文件
转自:http://hi.baidu.com/php_php_com/item/4de4b5fa51938842922af26b
页:
[1]