萝卜头IT论坛

搜索
查看: 2019|回复: 0
收起左侧

[水] php解压缩文件方法汇总

[复制链接]
发表于 2013-6-29 12:25:09 | 显示全部楼层 |阅读模式
一  使用内部扩展实现(>=php5)
  1. function unzip_file($file, $destination){
  2.   // create object
  3.   $zip = new ZipArchive() ;
  4.   // open archive
  5.   if ($zip->open($file) !== TRUE) {
  6.   die ('Could not open archive');
  7.   }
  8.   // extract contents to destination directory
  9.   $zip->extractTo($destination);
  10.   // close archive
  11.   $zip->close();
  12.   echo 'Archive extracted to directory';
  13. }
  14. unzip_file('./a.zip','bs');
复制代码
二   不使用组件    完全使用php内部函数
解压代码:
  1. public function ExtractFile($header,$to,$zip) {
  2.   $header = $this->readfileheader($zip);

  3.   if (substr($to,-1)!="/") $to.="/";
  4.   if ($to=='./') $to = '';
  5.   $pth = explode("/",$to.$header['filename']);
  6.   $mydir = '';
  7.   for($i=0;$i<count($pth)-1;$i++) {
  8.    if (!$pth[$i]) continue;
  9.    $mydir .= $pth[$i]."/";
  10.    if ((!is_dir($mydir) && @mkdir($mydir,0777)) || (($mydir==$to.$header['filename'] || ($mydir==$to && $this->total_folders==0)) && is_dir($mydir)) ) {
  11.     @chmod($mydir,0777);
  12.     $this->total_folders ++;
  13.     echo 'Extract : ',$mydir,'<br>';
  14.    }
  15.   }

  16.   if (strrchr($header['filename'],'/')=='/') return;
  17.   if (!($header['external']==0x41FF0010)&&!($header['external']==16)) {
  18.    if ($header['compression']==0) {
  19.     $fp = @fopen($to.$header['filename'], 'wb');
  20.     if (!$fp) return(-1);
  21.     $size = $header['compressed_size'];
  22.     while ($size != 0) {
  23.      $read_size = ($size < 2048 ? $size : 2048);
  24.      $buffer = fread($zip, $read_size);
  25.      $binary_data = pack('a'.$read_size, $buffer);
  26.      @fwrite($fp, $binary_data, $read_size);
  27.      $size -= $read_size;
  28.     }
  29.     fclose($fp);
  30.     touch($to.$header['filename'], $header['mtime']);
  31.    } else {
  32.     $fp = @fopen($to.$header['filename'].'.gz','wb');
  33.     if (!$fp) return(-1);
  34.     $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']),
  35.     Chr(0x00), time(), Chr(0x00), Chr(3));

  36.     fwrite($fp, $binary_data, 10);
  37.     $size = $header['compressed_size'];

  38.     while ($size != 0) {
  39.      $read_size = ($size < 1024 ? $size : 1024);
  40.      $buffer = fread($zip, $read_size);
  41.      $binary_data = pack('a'.$read_size, $buffer);
  42.      @fwrite($fp, $binary_data, $read_size);
  43.      $size -= $read_size;
  44.     }

  45.     $binary_data = pack('VV', $header['crc'], $header['size']);
  46.     fwrite($fp, $binary_data,8); fclose($fp);

  47.     $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);//删除文件


回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-5-2 01:37 , Processed in 0.099832 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表