Home > Backend Development > PHP Problem > How to solve the problem of garbled characters in php zip decompression

How to solve the problem of garbled characters in php zip decompression

藏色散人
Release: 2023-03-05 07:18:01
Original
4078 people have browsed it

php zip decompression garbled solution: first transcode the Chinese compressed package name using the iconv function; then process the zip file through the ZipArchive object; and finally close the processed zip file.

How to solve the problem of garbled characters in php zip decompression

## Recommended: "

PHP Video Tutorial"

The test environment is windows7

The Chinese zip package name and Chinese file name will cause the ZipArchive native to PHP 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
Copy after login

Refer 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
Copy after login

But when $zipfile is Chinese encoding, that is When the name of the compressed package is Chinese, decompression will still fail.

At this timeuse the iconv function to transcode the Chinese compressed package name separately before using 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;;
        }
Copy after login

上述代码的输出:

Array
(
    [0] => .
    [1] => ..
    [2] => ��
    [3] => Ҫ
)
Copy after login

压缩包的目录:

 中文.zip
├── 不
│   └── 要
│       └── dance.txt
└── 要
    └── dance1.txt
Copy after login

The above is the detailed content of How to solve the problem of garbled characters in php zip decompression. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template