Home  >  Article  >  Backend Development  >  How to solve the problem of garbled Chinese file names in PHP compression

How to solve the problem of garbled Chinese file names in PHP compression

藏色散人
藏色散人Original
2021-06-03 09:09:563655browse

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.

How to solve the problem of garbled Chinese file names in PHP compression

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[&#39;crc&#39;] == 0) {
        //新建目录
        mkdir($toDir.&#39;/&#39;.substr($statInfo[&#39;name&#39;], 0,-1));
    } else {
        //拷贝文件
        copy(&#39;zip://&#39;.$zipfile.&#39;#&#39;.$statInfo[&#39;name&#39;], $toDir.&#39;/&#39;.$statInfo[&#39;name&#39;]);
    }
}
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 = &#39;中文.zip&#39;;
        //zip文件相对服务器根目录的保存路径
        $uploads_dir = "/Public/zipUpload";
        //zip文件完整的保存路径
        $zipName = $_SERVER[&#39;DOCUMENT_ROOT&#39;].$uploads_dir.&#39;/&#39;.$fileName;
        //将目标路径名称赋值为fileName最后的&#39;.zip&#39;四个字符之外的全部字符构成的字符串
        $toDir = $_SERVER[&#39;DOCUMENT_ROOT&#39;].$uploads_dir.&#39;/&#39;.substr($fileName,0,strlen($fileName)-4);
        $zip = new \ZipArchive;//新建一个ZipArchive的对象
        /*
        通过ZipArchive的对象处理zip文件
        $zip->open这个方法的参数表示处理的zip文件名。
        如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
        */
        $res = $zip->open(iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;, $zipName));
        if ($res === TRUE){
            if (!is_dir(iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;, $toDir))) {
                mkdir(iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;, $toDir), 0777, true);
            }
            //$zip->extractTo($toDir);
            $docnum = $zip->numFiles;
            for($i = 0; $i < $docnum; $i++) {
                $statInfo = $zip->statIndex($i);
                if($statInfo[&#39;crc&#39;] == 0) {
                    //新建目录
                    mkdir(iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;, $toDir.&#39;/&#39;.$statInfo[&#39;name&#39;]), 0777, true);
                } else {
                    //拷贝文件,特别的改动,iconv的位置决定copy能不能work
                    if(copy(&#39;zip://&#39;.iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;, $zipName).&#39;#&#39;.$statInfo[&#39;name&#39;], iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;, $toDir.&#39;/&#39;.$statInfo[&#39;name&#39;])) == false){
                        echo &#39;faild to copy&#39;;
                    }
                }
            }
            print_r(scandir(iconv ( &#39;UTF-8&#39;, &#39;GB2312&#39;,$toDir)));
            $zip->close();//关闭处理的zip文件
        }
        else{
            echo &#39;failed, code:&#39;.$res.&#39;<br>&#39;;
        }

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn