I have a file that I want to add to a Zip with PHP, which is encoded in UTF-8. Here is the filename: 'µ漢字ääÖÅ.txt'.
Now, to get this file even to save to the filesystem I had to do this:
$filename = "µ漢字ääÖÅ.txt";
$codepage = 'Windows-' . trim(strstr(setlocale(LC_CTYPE, 0), '.'), '.');
$encoded_filename = iconv('UTF-8', $codepage.'//IGNORE', $filename);
file_put_contents($encoded_filename, "text");
Now when I want to add the file to the ziparchive I use the following code:
$zip = new \ZipArchive();
$zip->open('test.zip', \ZIPARCHIVE::CREATE);
$zip->addFile($encoded_filename, $encoded_filename);
$zip->close();
Which results in a zip file containing the file name 'ÁõõÍ+.txt'. How do I get it to save correctly?
I have found an answer (sort of). In the example above $encoded_filename
was changed from UTF-8 to Windows-1252 encoding to save to the filesystem. I have no idea why but Windows-1252 works when saving directly to the filesystem but NOT when saving to a zip using ZipArchive.
To fix this I had to encode the $filename yet again to a different encoding, CP858.
Example:
$filename = "µ漢字ääÖÅ.txt";
//encode to windows-1252 to save to the filesystem
$encoded_filename = iconv("UTF-8","Windows-1252//IGNORE",$filename);
file_put_contents($encoded_filename, "text");
//put in a zip file
$zip = new \ZipArchive();
$zip->open('test.zip', \ZIPARCHIVE::CREATE);
//encode as CP858 to save into zip file
$zip->addFile($encoded_filename, iconv("UTF-8", "CP858//IGNORE", $filename));
$zip->close();
In the above example it still doesn't handle the Japanese characters in the file name, but at least it handles European characters which will have to do for the time being.