/*
* 解压文件
* 需开启配置 php_zip.dll
* filename 要解压的文件全路径
* path 解压文件后保存路径
* 返回值 trur或者false
* 作者:kTWO
* 时间:2016-12-11 15:50:20
* 网址:https://www.k2zone.cn/
*/
function get_zip($filename, $path) {
$filename = iconv("utf-8", "gb2312", $filename); //中文路径转码
$path = iconv("utf-8", "gb2312", $path);
if (!file_exists($filename)) { //先判断待解压的文件是否存在
die("文件 $filename 不存在!");
return false;
}
$resource = zip_open($filename); //打开压缩包
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, "/")); //以最后一个“/”分割,再用字符串截取出路径部分
if (!is_dir($file_path)) { //如果路径不存在,则创建一个目录,true表示可以创建多级目录
mkdir($file_path, 0777, true);
}
if (!is_dir($file_name)) { //如果不是目录,则写入文件
$file_size = zip_entry_filesize($dir_resource); //读取这个文件
if ($file_size < (1024 * 1024 * 200)) { //最大读取200M,如果文件过大,跳过解压,继续下一个
$file_content = zip_entry_read($dir_resource, $file_size);
file_put_contents($file_name, $file_content);
} else {
return false;
}
}
zip_entry_close($dir_resource); //关闭当前
}
}
zip_close($resource); //关闭压缩包
return true;
}
$x=get_zip("index.zip","public/");