php zip内容编码 [英] php zip contents encoding

查看:120
本文介绍了php zip内容编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UTF-8编码通过php脚本创建一些.txt文件(带希腊字符)。
当我试图从ftp下载和读取这些文件时,一切正常(无编码问题)。

I create some .txt files (with greek characters) via a php script, with UTF-8 encoding. When i am trying to download and read these files from ftp everything works fine (no encoding problems).

当我将这些文件压缩到zip存档时,我正面临着所有希腊字符和newLines字符的编码问题。

When I zip these files to a zip archive, I am facing encoding problem with all greek characters and newLines chars.

zip脚本:

    $zip = new ZipArchive();
    $filename = "my_zip.zip";

    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
        exit("cannot open <$filename>\n");
    }


    $files = glob('users/'.$_SESSION['s_uid'].'/*'); // get all file names
    foreach($files as $file){ // iterate files
        if(is_file($file))
            //$zip->addFile($thisdir . $file, basename($file));
            $zip->addFile($thisdir . $file, iconv("UTF-8","UTF-8", basename($file)));
            //echo $file; // delete file
    }   
    $zip->close();


推荐答案

我也遇到了ZIP存档提取的问题UTF8字符。我没有找到任何真正的解决方案这个问题(我认为PECL中的问题仍然存在,因为我使用5.6) https://bugs.php.net/bug.php?id=53948

I also came across the problem of ZIP archive extraction with UTF8 characters. I have not been able to find any real solution to this problem (I think that problem in PECL still exists since I am working with 5.6) https://bugs.php.net/bug.php?id=53948

...所以我来了包括在解压缩文件之前重新命名归档文件的解决方法。有了这个解决方法,我仍然可以允许用户在其归档文件中上传UTF8文件名,但是我只是用正则表达式替换特殊字符。

... so I came up with a workaround which involves renaming archive files before extracting them. With this workaround I can still allow users to upload UTF8 file names in their archive, but I just replace special char with regex.

            $zip = new ZipArchive;
            if ($zip->open($file_path) === true) {
                for ($i = 0; $i < $zip->numFiles; $i++) {
                    $fname = $zip->getNameIndex($i);      
                    $fname_new = // your str replacemenet code...
                    $zip->renameIndex($i, $fname_new);

这篇关于php zip内容编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆