大部分在线上传解压可能中文文件名乱码 以下可能是解决关键
默认识别的编码比如EUC-CN 实际是gb2312?CP936实际是gbk。?
<?php
header('Content-type:text/html;charset=utf-8');
if(!function_exists('mb_detect_encoding')) exit("No mb_detect_encoding()");
$arr=array('EUC-CN' => 'GB2312','CP936' => 'GBK','SJIS-mac'=>'MacJapanese','SJIS-Mobile#DOCOMO'=>'SJIS-DOCOMO','SJIS-Mobile#KDDI'=>'SJIS-KDDI','SJIS-Mobile#SOFTBANK'=>'SJIS-SOFTBANK','UTF-8-Mobile#DOCOMO'=>'UTF-8-DOCOMO','UTF-8-Mobile#KDDI-B'=>'UTF-8-KDDI','UTF-8-Mobile#SOFTBANK'=>'UTF-8-SOFTBANK','ISO-2022-JP-MOBILE#KDDI'=>'ISO-2022-JP-KDDI');
$zipfile = 'LW1X_V210814_080158.zip'; //修改为要解压的文件名
$zipDir = "./";
$zip = new ZipArchive();
if ($zip->open($zipfile) == true) {
$rf = zip_open($zipfile); $i = 0;
while ($fr = zip_read($rf)) {
$fileInfo = $zip->statIndex($i);
$fileName=$fileInfo['name'];
if($fileName!="null" && !stristr("--/-\-","-{$fileName}-")){ //无效文件过滤
$encode = mb_detect_encoding($fileName, ['ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5']);
if(array_key_exists($encode, $arr)) $encode = $arr[$encode]; //特殊编码处理
$fileName = mb_convert_encoding($fileName, 'UTF-8', $encode);
echo "<p>$i \t ".$fileName."</p>";
$toDir = $zipDir . $fileName; $teDir = dirname($toDir);
$content = zip_entry_read($fr, zip_entry_filesize($fr));
if (!is_dir($teDir)) { mkdir($teDir, 755, true);}
file_put_contents($toDir, $content);
}
$i++;
}
}
$zip->close();
|