Home > Article > Backend Development > 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(' ',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(' ',microtime()); //解压结束的时间 $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); $thistime = round($thistime,3); //保留3为小数 echo "<p>解压完毕!,本次解压花费:$thistime 秒。</p>"; } $size = get_zip_originalsize('../../textaa.zip','../../ffff/');
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('test.zip') === TRUE) {//中文文件名要使用ANSI编码的文件格式 $zip->extractTo('foldername');//提取全部文件 //$zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));//提取部分文件 $zip->close(); echo 'ok'; } else { echo 'failed'; }
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!