php zip contents encoding

我与影子孤独终老i 提交于 2020-01-14 14:42:50

问题


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).

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

zip script:

    $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();

回答1:


That appears to be a unresolved bug in PHP not being able to handle UTF-8 encoded file names.

  • https://bugs.php.net/bug.php?id=53948
  • http://tracker.moodle.org/browse/MDL-24928

There is also a similar question on this.

Problem when zipping files with special characters in PHP+Apache - encoding issue




回答2:


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

... 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);


来源:https://stackoverflow.com/questions/13267143/php-zip-contents-encoding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!