$filePath: 压缩包路径 ../123.zip
$path:要解压的目录 ../unzip/
public function newUnzip($filePath,$path)
{
$zip= new \ZipArchive();
if($zip->open($filePath) === true) {
// 创建要解压的目录
// 获取解压的文件数组
$fileNum = $zip->numFiles;
for ($i = 0; $i < $fileNum; $i++) {
// 处理每一个文件
$statInfo = $zip->statIndex($i, 64);
// 获取文件的名称的编码格式
$info = explode('/', $statInfo['name']);
foreach ($info as $key => $val) {
$encoding = mb_detect_encoding($val, ['UTF-8', 'GBK', 'BIG5', 'CP936']);
$info[$key] = iconv($encoding, 'UTF-8//IGNORE', $val);
}
$statInfo['name'] = implode('/', $info);
mkdir($path, 0777, true);
if (strpos($statInfo['name'], '/') !== false) {
$mkdFile = explode('/', $statInfo['name']);
if (count($mkdFile) == 1) {
$namePath = $mkdFile[0];
} else {
$namePath = $mkdFile[count($mkdFile) - 1];
}
//获取zip单个文件数据
$fp = fopen('zip://' . $filePath . '#' . $zip->getNameIndex($i), 'rb');
$create = true;
while (!feof($fp)) {
//fread 获取文件数据流 保存到本地
file_put_contents($path . DIRECTORY_SEPARATOR . $namePath, fread($fp, 9999999),$create ? 0 : FILE_APPEND);
$create = false;
}
} else {
if (!copy('zip://' . $filePath . '#' . $zip->getNameIndex($i), $path . '/' . $statInfo['name'])) {
return false;
}
}
}
}
$zip->close();
}