一.不考虑乱码
$zip = new \ZipArchive(); if ($zip->open($path) === TRUE) { $zip->extractTo($dir_path_pu); $zip->close(); } else { $this->error('文件解压失败'); }
二.乱码问题解决
$zip = new \ZipArchive(); if ($zip->open($path) === TRUE) { for ($i = 0; $i < $zip->numFiles; $i++) { $statInfo = $zip->statIndex($i, \ZipArchive::FL_ENC_RAW); $encoding = mb_detect_encoding($statInfo['name'], ['UTF-8', 'GBK', 'BIG5', 'CP936']); // 转换成UTF-8编码 if (DIRECTORY_SEPARATOR == '/') { //linux $filename = iconv($encoding, 'UTF-8', $statInfo['name']); } else { //win $filename = iconv($encoding, 'UTF-8', $statInfo['name']); } if (!is_dir($dir_path)) { mkdir($dir_path, 0777, true); } if (!copy('zip://' . $path . '#' . $zip->getNameIndex($i), $dir_path . '/' . $filename)) { return false; } } // $zip->extractTo($dir_path_pu); $zip->close(); } else { $this->error('文件解压失败'); }
三.多层级时
if($statInfo['crc'] == 0) { //新建目录 //mkdir($path.'/'.substr($filename, 0,-1)); mkdir($path, 0777, true); } else { if(strpos($filename,'/') !== false){ //获取文件名(看自己需求) $mkdFile = explode('/',$filename); if(count($mkdFile) == 1){ $namePath = $mkdFile[0]; }else{ $namePath = $mkdFile[count($mkdFile)-1]; } //获取zip单个文件数据 $fp = fopen('zip://' . $path . '#' . $zip->getNameIndex($i), 'rb'); while (!feof($fp)) { //fread 获取文件数据流 保存到本地 file_put_contents($dir_path.DIRECTORY_SEPARATOR.$namePath, fread($fp,9999999)); } }else{ if(!copy('zip://' . $path . '#' . $zip->getNameIndex($i), $dir_path . '/' . $filename)){ return false; } } }