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

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

藏色散人
藏色散人Original
2020-08-24 10:37:493870browse

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:

open($zipfile);
$toDir = "./test";
$s = $zip->extractTo($toDir);
print_r(scandir($toDir));
//作者:Dorm_Script
//链接:http://www.jianshu.com/p/3efa924fd809

Refer to a blog, the original solution is:

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 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 = '中文.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.'
'; }

上述代码的输出:

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

压缩包的目录:

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

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!

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