您现在的位置是:群英 > 开发技术 > PHP语言
PHP用ZipArchive解压造成中文乱码如何处理
Admin发表于 2022-12-01 16:15:091145 次浏览
在实际案例的操作过程中,我们可能会遇到“PHP用ZipArchive解压造成中文乱码如何处理”这样的问题,那么我们该如何处理和解决这样的情况呢?这篇小编就给大家总结了一些方法,具有一定的借鉴价值,希望对大家有所帮助,接下来就让小编带领大家一起了解看看吧。


php压缩中文乱码的解决方法:1、通过“composer require nelexa/zip”安装PhpZip;2、打开“ZipFile.php”;3、找到“extractTo”方法,将“is_writable”函数改掉或去掉;4、转换一下编码格式即可。

怎么解决php压缩中文乱码问题?

解决php使用ZipArchive解压时中文乱码问题(纯php,绕开ZipArchive)

解决php使用ZipArchive解压时中文乱码问题

使用php自带的ZipArchive来解压带中文文件名压缩包时会造成乱码,现象如下:

网上查阅基本上给出的答案大同小异,自己照着同样的方法试了都不能解决,下图是网上给出的方案:

经过摸索终于找到了解决方案,那就是弃用ZipArchive,选择其他的途径,经过比较我选择了“PhpZip”,优点是:纯php(不需要扩展和类),下面介绍安装方法:

composer安装:

composer require nelexa/zip
登录后复制

如果选择版本的话:composer require nelexa/zip:^3.0 (3.0版本号)

使用方法:

//解压文件
$fileAddess = "压缩包地址";
$toDir = "解压目录";
$zipFile = new \PhpZip\ZipFile();
$zipFile->openFile($fileAddess) // open archive from file
->extractTo($toDir); // extract files to the specified directory
$zipFile->close();
登录后复制

注释:(**如果安装好使用正常请忽略下面的注释内容**)

1.我的压缩包放在共享盘里面连接的是smb地址,可能会出现报错:"Destination is not writable directory",这个坑已经踩过:打开ZipFile.php这个文件,找到“extractTo”方法,将“is_writable”函数改掉,或者去掉,然后再转换一下编码格式!,下面有注释的是我修改的内容!

/**
     * Extract the archive contents
     *
     * Extract the complete archive or the given files to the specified destination.
     *
     * @param string $destination Location where to extract the files.
     * @param array|string|null $entries The entries to extract. It accepts either
     *                                   a single entry name or an array of names.
     * @return ZipFile
     * @throws ZipException
     */
    public function extractTo($destination, $entries = null)
    {
        if (!file_exists($destination)) {
            throw new ZipException("Destination " . $destination . " not found");
        }
        if (!is_dir($destination)) {
            throw new ZipException("Destination is not directory");
        }
        $is_really_writable = $this->is_really_writable($destination);
        if (!$is_really_writable) {
            throw new ZipException("Destination is not writable directory");
        }
        /**
         * @var ZipEntry[] $zipEntries
         */
        if (!empty($entries)) {
            if (is_string($entries)) {
                $entries = (array)$entries;
            }
            if (is_array($entries)) {
                $entries = array_unique($entries);
                $flipEntries = array_flip($entries);
                $zipEntries = array_filter(
                    $this->centralDirectory->getEntries(),
                    function ($zipEntry) use ($flipEntries) {
                        /**
                         * @var ZipEntry $zipEntry
                         */
                        return isset($flipEntries[$zipEntry->getName()]);
                    }
                );
            }
        } else {
            $zipEntries = $this->centralDirectory->getEntries();
        }
        foreach ($zipEntries as $entry) {
            /******************************王天佑添加的逻辑start************************************/
            header("Content-type:text/html;charset=bgk");
            $entry_getName = iconv('GB2312', 'UTF-8//ignore',$entry->getName());
            //header("Content-type:text/html;charset=utf-8");
            /******************************王天佑添加的逻辑start************************************/
            $file = $destination . DIRECTORY_SEPARATOR . $entry_getName;
            if ($entry->isDirectory()) {
                if (!is_dir($file)) {
                    if (!mkdir($file, 0755, true)) {
                        throw new ZipException("Can not create dir " . $file);
                    }
                    chmod($file, 0755);
                    touch($file, $entry->getTime());
                }
                continue;
            }
            $dir = dirname($file);
            if (!is_dir($dir)) {
                if (!mkdir($dir, 0755, true)) {
                    throw new ZipException("Can not create dir " . $dir);
                }
                chmod($dir, 0755);
                touch($dir, $entry->getTime());
            }
            if (file_put_contents($file, $entry->getEntryContent()) === false) {
                throw new ZipException('Can not extract file ' . $entry_getName);
            }
            touch($file, $entry->getTime());
        }
        return $this;
    }
    //王天佑加的新逻辑,判断目录是否可写
    public function is_really_writable($file){
        if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE) {
            return is_writable($file);
        }
        if (is_dir($file)) {
            $file = rtrim($file, '/') . '/' . md5(mt_rand(1,100) . mt_rand(1,100));
            if (($fp = @fopen($file, "w+")) === FALSE) {
                return FALSE;
            }
            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);
        } elseif (!is_file($file) OR ($fp = @fopen($file, "r+")) === FALSE) {
            fclose($fp);
            return FALSE;
        }
        return TRUE;
    }
登录后复制



关于“PHP用ZipArchive解压造成中文乱码如何处理”的内容就介绍到这,感谢各位的阅读,相信大家对PHP用ZipArchive解压造成中文乱码如何处理已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-09-05 17:43:51 
摘要:这篇文章主要介绍了Yii 框架入口脚本,结合实例形式分析了Yii 框架入口脚本基本功能、原理及相关操作技巧,需要的朋友可以参考下
2021-12-14 11:11:03 
摘要:这篇文章给大家分享的是PHP多线程编的实现。小编觉得挺实用的,因此分享给大家做个参考,下文将介绍多线程及适用常见、PHP实现多线程的方法等等,示例介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
2022-07-04 17:29:46 
摘要:这篇文章主要为大家介绍了Go语言基础枚举的用法及示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部