PHP解压ZIP压缩包,解决中文乱码问题
不浪费时间了,直接上代码吧
<?php
header('Content-type:text/html;charset=utf-8');
$zip = new ZipArchive();
$zipfile = '中文.zip';
$toDir = './' . mb_strrchr($zipfile, '.', true) . '/';
if ($zip->open($zipfile) == true) {
$rf = zip_open($zipfile);
$i = 0;
while ($fr = zip_read($rf)) {
$fileInfo = $zip->statIndex($i, ZipArchive::FL_ENC_RAW);
//转码 否则中文会出现乱码
$encode = mb_detect_encoding($fileInfo['name'], ['ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5']);
if ($encode === false) {
return '未识别到该字符编码';
}
$fileName = mb_convert_encoding($fileInfo['name'], 'UTF-8', $encode);
//如果是目录
if ($fileInfo['crc'] == 0) {
//新建目录
$dir = $toDir . $fileName;