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;
if (!file_exists($dir)) {
mkdir($dir, 777, true);
}
} else {
//读取文件内容并写入
$content = zip_entry_read($fr, zip_entry_filesize($fr));

本文介绍如何使用PHP的ZipArchive类解压ZIP文件,并解决解压过程中可能出现的中文乱码问题。通过提供的代码示例,可以有效避免文件名乱码,确保正确解压含有中文名称的文件。
最低0.47元/天 解锁文章
351

被折叠的 条评论
为什么被折叠?



