php zip contents encoding

2019-07-15 10:58发布

问题:

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


标签: php encoding zip