php ZipArchive读取压缩包文件存在压缩包中文乱码解决方法
·
压缩包文件名提前转码一下就好了
/**
* 重命名压缩包里面的文件名,主要是避免文件名读取的时候乱码
*
* @param UploadedFile $file
* @return array
*/
private function reZipFileName(UploadedFile $file)
{
$res = [
'isError'=>false,
'msg'=>''
];
$zip = new \ZipArchive();
$openZipRes = $zip->open($file->getRealPath());
if ($openZipRes !== true){
$res = [
'isError'=>true,
'msg'=>'无法打开文件'
];
return $res;
}
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的转换成UTF-8,避免读取的时候乱码,CP936是简体中文编码
// 一般本地电脑压缩文件存在中文的文件名编码是CP936,但是用winRAR软件压缩编码是UTF-8,读取时候不乱码
if($encoding!= 'UTF-8'){
$zip->renameIndex($i, iconv($encoding, 'UTF-8', $statInfo['name']));
}
}
$zip->close();
return $res;
}
更多推荐


所有评论(0)