Home  >  Article  >  Backend Development  >  How to decompress compressed package in php

How to decompress compressed package in php

王林
王林Original
2019-09-21 17:58:355703browse

How to decompress compressed package in php

php implements decompression of ZIP compressed files

First: You need to enable the configuration of php_aip.dll

<?php
//需开启配置 php_zip.dll
//phpinfo();
header("Content-type:text/html;charset=utf-8");

function get_zip_originalsize($filename, $path) {
  //先判断待解压的文件是否存在
  if(!file_exists($filename)){
    die("文件 $filename 不存在!");
  }
  $starttime = explode(&#39; &#39;,microtime()); //解压开始的时间

  //将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到
  $filename = iconv("utf-8","gb2312",$filename);
  $path = iconv("utf-8","gb2312",$path);
  //打开压缩包
  $resource = zip_open($filename);
  $i = 1;
  //遍历读取压缩包里面的一个个文件
  while ($dir_resource = zip_read($resource)) {
    //如果能打开则继续
    if (zip_entry_open($resource,$dir_resource)) {
      //获取当前项目的名称,即压缩包里面当前对应的文件名
      $file_name = $path.zip_entry_name($dir_resource);
      //以最后一个“/”分割,再用字符串截取出路径部分
      $file_path = substr($file_name,0,strrpos($file_name, "/"));
      //如果路径不存在,则创建一个目录,true表示可以创建多级目录
      if(!is_dir($file_path)){
        mkdir($file_path,0777,true);
      }
      //如果不是目录,则写入文件
      if(!is_dir($file_name)){
        //读取这个文件
        $file_size = zip_entry_filesize($dir_resource);
        //最大读取6M,如果文件过大,跳过解压,继续下一个
        if($file_size<(1024*1024*30)){
          $file_content = zip_entry_read($dir_resource,$file_size);
          file_put_contents($file_name,$file_content);
        }else{
          echo "<p> ".$i++." 此文件已被跳过,原因:文件过大, -> ".iconv("gb2312","utf-8",$file_name)." </p>";
        }
      }
      //关闭当前
      zip_entry_close($dir_resource);
    }
  }
  //关闭压缩包
  zip_close($resource);
  $endtime = explode(&#39; &#39;,microtime()); //解压结束的时间
  $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
  $thistime = round($thistime,3); //保留3为小数
  echo "<p>解压完毕!,本次解压花费:$thistime 秒。</p>";
}

$size = get_zip_originalsize(&#39;../../textaa.zip&#39;,&#39;../../ffff/&#39;);

In the above method: $filename: file name, is the name of the file to be decompressed, including the path relative to the method, $path represents: decompress to (what directory, ending with / ).

The second method is: use the class that comes with pclzip. The first method was adopted during the project due to path problems

<?php
/*
php 从zip压缩文件中提取文件
*/
$zip = new ZipArchive;

if ($zip->open(&#39;test.zip&#39;) === TRUE) {//中文文件名要使用ANSI编码的文件格式
  $zip->extractTo(&#39;foldername&#39;);//提取全部文件
  //$zip->extractTo(&#39;/my/destination/dir/&#39;, array(&#39;pear_item.gif&#39;, &#39;testfromfile.php&#39;));//提取部分文件
  $zip->close();
  echo &#39;ok&#39;;
} else {
  echo &#39;failed&#39;;
}

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to decompress compressed package in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How php solves big dataNext article:How php solves big data