The solution to the garbled Chinese file name in PHP compression: first transcode the Chinese compressed package name using the iconv function; then process the zip file through the ZipArchive object.
The operating environment of this article: Windows 10 system, PHP version 7.1, DELL G3 computer
PHP decompresses the ZIP compressed package to solve the problem of Chinese garbled characters
Fix the Chinese garbled problem of native ZipArchive
The Chinese zip archive name and Chinese file name will cause PHP's native ZipArchive to be garbled, causing the extractTo method to fail:
<?php $zip = new \ZipArchive; $zipfile = "./test.zip"; $res = $zip->open($zipfile); $toDir = "./test"; $s = $zip->extractTo($toDir); print_r(scandir($toDir)); //作者:Dorm_Script //链接:http://www.jianshu.com/p/3efa924fd809
Referring to a blog, the original solution is:
<?php $zip = new \ZipArchive; $zipfile = "./test.zip"; $res = $zip->open($zipfile); $toDir = "./test"; if(!file_exists($toDir)) { mkdir($toDir); } $docnum = $zip->numFiles; for($i = 0; $i < $docnum; $i++) { $statInfo = $zip->statIndex($i); if($statInfo['crc'] == 0) { //新建目录 mkdir($toDir.'/'.substr($statInfo['name'], 0,-1)); } else { //拷贝文件 copy('zip://'.$zipfile.'#'.$statInfo['name'], $toDir.'/'.$statInfo['name']); } } print_r(scandir($toDir)); //作者:Dorm_Script //链接:http://www.jianshu.com/p/3efa924fd809
But when $zipfile is in Chinese encoding, that is, when the name of the compressed package is in Chinese, decompression will still fail.
At this time, use the iconv function to transcode the Chinese compressed package name and then use it:
//zip文件名 $fileName = '中文.zip'; //zip文件相对服务器根目录的保存路径 $uploads_dir = "/Public/zipUpload"; //zip文件完整的保存路径 $zipName = $_SERVER['DOCUMENT_ROOT'].$uploads_dir.'/'.$fileName; //将目标路径名称赋值为fileName最后的'.zip'四个字符之外的全部字符构成的字符串 $toDir = $_SERVER['DOCUMENT_ROOT'].$uploads_dir.'/'.substr($fileName,0,strlen($fileName)-4); $zip = new \ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表示处理的zip文件名。 如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE */ $res = $zip->open(iconv ( 'UTF-8', 'GB2312', $zipName)); if ($res === TRUE){ if (!is_dir(iconv ( 'UTF-8', 'GB2312', $toDir))) { mkdir(iconv ( 'UTF-8', 'GB2312', $toDir), 0777, true); } //$zip->extractTo($toDir); $docnum = $zip->numFiles; for($i = 0; $i < $docnum; $i++) { $statInfo = $zip->statIndex($i); if($statInfo['crc'] == 0) { //新建目录 mkdir(iconv ( 'UTF-8', 'GB2312', $toDir.'/'.$statInfo['name']), 0777, true); } else { //拷贝文件,特别的改动,iconv的位置决定copy能不能work if(copy('zip://'.iconv ( 'UTF-8', 'GB2312', $zipName).'#'.$statInfo['name'], iconv ( 'UTF-8', 'GB2312', $toDir.'/'.$statInfo['name'])) == false){ echo 'faild to copy'; } } } print_r(scandir(iconv ( 'UTF-8', 'GB2312',$toDir))); $zip->close();//关闭处理的zip文件 } else{ echo 'failed, code:'.$res.'<br>'; }
The output of the above code:
Array ( [0] => . [1] => .. [2] => �� [3] => Ҫ )
The directory of the compressed package:
中文.zip
├── No
│ └── Want
│ └── dance.txt
└── To
└── dance1.txt
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of garbled Chinese file names in PHP compression. For more information, please follow other related articles on the PHP Chinese website!