encoding - 用PHP如何检测一个ZIP包内的文件是在何种编码的系统下创建的
PHP中文网
PHP中文网 2017-04-10 14:25:17
0
5
660

事情起源于在WIN下创建的zip放到linux下解压时,中文路径和文件名会出现乱码,于是动手写了个脚本转换zip内文件名的代码。但是,如果是在日语、韩语或者繁体字WIN系统下建立的zip,由于不知道原始编码格式,导致无法转码。
怎么解。。。

PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆 (5)
PHPzhong

LZ 的 id 看着眼熟 ... 这么多年了还在问这个等级的问题 ... 你也不容易啊你 ...

open( '/path/to/your.zip' ); /* can not open ..? are you kidding me ..? */ if ( true !== $res ) throw new Exception( 'Can Not Open Zip File / ' . $res ); /* default value of file encoding ... */ $encoding = 'EMTPY'; /* controller ... change this if mb_detect_encoding return wrong answer ... */ $controller = null; /* get file list ... */ for ( $i = 0; $i < $zip->numFiles; ++ $i ) { /* get file encoding ... */ $encoding = mb_detect_encoding( $zip->getNameIndex( $i ), $controller ); /* we do not need english named files ... */ if ( 'ASCII' !== $encoding ) break; } /* clean table ... */ $zip->close(); /* simply output ... */ echo $encoding;

代码就是这样了 ... 根据文件名来判断系统 ...

简体中文的 windows 会返回 EUC-CN ... 繁体中文我猜测应该是 EUC-TW 或者 BIG5 ...

Linux 和 MacOS 都是 UTF-8 ... 纯英文的文件就别捣乱了 ...

    左手右手慢动作

    应该是有编码问题,举例说明:mac下的压缩,如果文件是中文名,到win下面解压的时候全部是乱码。所以在mac下面压缩的时候,都尽量只用英文文件名。

    11年的时候,有人提过类似问题:http://bbs.csdn.net/topics/370123319 ,文中的解决方案是说:查看http://www.pkware.com/documents/cases...,并搜索“Info-ZIP Unicode Path Extra Field”

    搜索完这段内容:

    4.6 Third Party Mappings ------------------------ 4.6.1 Third party mappings commonly used are: 0x07c8 Macintosh 0x2605 ZipIt Macintosh 0x2705 ZipIt Macintosh 1.3.5+ 0x2805 ZipIt Macintosh 1.3.5+ 0x334d Info-ZIP Macintosh 0x4341 Acorn/SparkFS 0x4453 Windows NT security descriptor (binary ACL) 0x4704 VM/CMS 0x470f MVS 0x4b46 FWKCS MD5 (see below) 0x4c41 OS/2 access control list (text ACL) 0x4d49 Info-ZIP OpenVMS 0x4f4c Xceed original location extra field 0x5356 AOS/VS (ACL) 0x5455 extended timestamp 0x554e Xceed unicode extra field 0x5855 Info-ZIP UNIX (original, also OS/2, NT, etc) 0x6375 Info-ZIP Unicode Comment Extra Field 0x6542 BeOS/BeBox 0x7075 Info-ZIP Unicode Path Extra Field 0x756e ASi UNIX 0x7855 Info-ZIP UNIX (new) 0xa220 Microsoft Open Packaging Growth Hint 0xfd4a SMS/QDOS

    希望有用。

      阿神

      @Ven 就是文件名的编码吧,稍微改了下楼上的代码,我的系统是linux,所以要把非UTF-8的重新编码为UTF-8

      open($zipfile_name); if(true !== $res) throw new Exception('Can Not Open Zip File '.$res); $encoding = "UTF-8"; $controller = array("ASCII","UTF-8", "GB2312", "GBK", "BIG5"); for($i = 0; $i < $zip->numFiles; ++ $i){ $entry = $zip->getNameIndex($i); $encoding = mb_detect_encoding($entry, $controller); if( "UTF-8" !== $encoding) $entry = iconv($encoding, "UTF-8", $entry); echo $entry." ---> ".$encoding.chr(10); } $zip->close(); } detect_encoding($argv[1]); ?>
        巴扎黑

        正确答案见@Sunyanzi 的回答,这里再补充一些。
        由于Windows系统的历史原因,部分压缩软件生成的zip包,在用mb_detect_encoding()检查文件名的编码时,会得到类似“CP936”这样的结果。我当时在这里被搞晕了,以为函数没能正确检测到编码。实际上CP936是微软自己的一套标准,基本上等于GBK。
        而关于其他的“CP***”的编码对应关系,或许可以参看这篇文章:Windows代码页

          刘奇

          ZIP格式,似乎文件名没有编码这一说。
          至于你说的解压缩出现乱码,这是解压缩软件的问题……

            最新下載
            更多>
            網站特效
            網站源碼
            網站素材
            前端模板
            關於我們 免責聲明 Sitemap
            PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!