解决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)相关推荐

  1. linux unzip乱码,Linux 中unzip解压时中文乱码的解决办法

    Linux 中unzip解压时中文乱码的解决办法 Linux 中unzip解压时中文乱码的解决办法 当我们在linux中解压一个含有中文名字的压缩包如"资料.zip"时,如果直接使 ...

  2. 解决ubuntu中zip解压的中文乱码问题

    在解压windows传过来的zip文件时,才会出现乱码.所以,我用另一个方法解决中文乱码问题. 安装 代码: sudo apt-get install unar 12.04以下或者想编译安装的朋友请参 ...

  3. python rarfile不支持中文路径_python使用zipfile解压文件中文乱码问题

    中文在编程中真实后娘养的,各种坑爹,python3下中文乱码这个问题抓破了头皮,头疼.看了alex的文章,才有种恍然大悟的感觉(链接在底部). 一句话,就是转换成unicode,压缩前是什么编码,使用 ...

  4. mysql5.7 解压版 中文乱码_MySQL 5.7解压版安装、卸载及乱码问题的图文解决方法...

    1. 解压版的安装 (1). 下载压缩包并解压到一个磁盘位置 压缩包下载链接:https://dev.mysql.com/downloads/mysql/ 压缩包内容: (2). 写配置文件 复制my ...

  5. Vue-pako gizp解压,中文乱码解决

    项目中后端返回数据为加密的,需要gizp解压,发现可以使用pako来进行解压 npm install pako // 这里进行下载 在你需要使用的页面或者全局导入 import pako from ' ...

  6. windows 解压文件出现乱码 7zip解压文件中文乱码 解决方法

    按照如下教程,更改windows默认编码格式为utf-8之后. https://blog.csdn.net/qq_43780850/article/details/129122210 解压文件时,文件 ...

  7. android zip中文乱码,Android中解压zip中文乱码处理

    乱码原因分析 一般我们使用的Windows系统外部文件默认都是GBK编码方式,Mac系统默认的好像是UTF-8(同事的电脑就是,不知道其他人的是不是),Linux的没验证过,这里我讨论的是Window ...

  8. java解压中文乱码_java解压文件中文乱码怎么处理

    上次利用java自动的java.util.zip.ZipEntry和??java.util.zip.ZipFile来解压zip文件,今天发现程序在读取解压文件时居然报了空指针异常,debug程序后发现 ...

  9. Android中解压zip中文乱码处理

    乱码原因分析 一般我们使用的Windows系统外部文件默认都是GBK编码方式,Mac系统默认的好像是UTF-8(同事的电脑就是,不知道其他人的是不是),Linux的没验证过,这里我讨论的是Window ...

  10. linux下mkdirs方法乱码,linux 解压zip中文乱码问题

    本文用程序解决的的,有兴趣可以看一看,limux有解决中文乱码问题,,请去其他博客. 方法一:使用java importjava.io.File;importjava.io.FileOutputStr ...

最新文章

  1. Qt5.10.1在Windows平台下进行静态编译
  2. python 贴吧自动回复机-用python itchat写一个微信机器人自动回复
  3. 《Android开发从零开始》——16.Service学习(2)
  4. 2. 托管对象数据模型的基本知识(Core Data 应用程序实践指南)
  5. oracle 052考试,Oracle OCP认证考试题库解析052-5
  6. 【图像超分辨率】Remote Sensing Image Super-resolution: Challenges and Approaches
  7. centos安装docker显示 No package docker-ce available
  8. 背部辨识度极高!红米K30系列将率先采用高通5G处理器
  9. iPhone降价都救不回销量?苹果仍需努力!
  10. 网易云音乐android变臃肿,网易云音乐版权少,为什么用户还能突破8亿??
  11. 如何解决'chromedriver' executable needs to be in PATH.的问题
  12. Discuz安全之伪装后台,修改后台登陆页面信息
  13. Android 对ListView和RecyclerView的两个BaseAdapter封装分享
  14. cygwin解压linux软件,如何在Cygwin上安装unzip | 望天博客
  15. 第14课:走向技术管理者的4种方式
  16. CES 2020最佳科技产品官方入围名单!三星笑了
  17. 四路监控物联卡赋能卡友行车安全
  18. Hebutgo 7.21 git使用(alicode)
  19. C++模板/泛型编程
  20. 史上最全的中文词汇数据集

热门文章

  1. FishC《零基础学习python》笔记--第001讲:我和Python的第一次亲密接触
  2. 小黑计算机一级题库,小黑课堂计算机一级题库
  3. 利用anaconda 下载python所依赖的文件包
  4. windows server 2012 安装 VC14(VC2015) 安装失败解决方案
  5. 石油化工行业的MES系统解决方案
  6. linux cuda 编程指南,CUDA编程指南阅读笔记
  7. armeabi与armeabi-v7a的区别,绝对干货!
  8. 微积分学基本定理简介
  9. 分期手续费率转换成年利率
  10. 用python完成《商务与经济统计(第13版)》课后练习——第九章