Bug #71034 [Nab]: Can't add UTF-8 filenames as UTF-8 to ZIP archive
| From: | slavikca at gmail dot com | Date: | Mon, 07 Dec 2015 07:49:56 +0000 |
| Subject: | Bug #71034 [Nab]: Can't add UTF-8 filenames as UTF-8 to ZIP archive | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-197650@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71034&edit=1
ID: 71034
User updated by: slavikca at gmail dot com
Reported by: slavikca at gmail dot com
Summary: Can't add UTF-8 filenames as UTF-8 to ZIP archive
Status: Not a bug
Type: Bug
Package: Zip Related
Operating System: Ubuntu 14.04
PHP Version: 5.6.16
Block user comment: N
Private report: N
New Comment:
Spent hours troubleshooting this issue.
I went to Zip binary format definition,
And found, that bug is really in ... linux unzip utility.
So, yes, PHP (both 5.6 and 7.0) really creates filenames in archive in UTF-8.
So, this is not a bug.
Previous Comments:
------------------------------------------------------------------------
[2015-12-06 18:28:33] ab@php.net
@slavikca, i think we're both confused by that zip Unicode thing. But nope - if you terminal is
UTF-8, it will correctly show UTF-8 string like "ÑеÑÑ". I've explicitly
used string "\u{442}\u{435}\u{441}\u{442}123.txt" as file name in PHP, and I explicitly
see the string "ÑеÑÑ123.txt" in the terminal when used "unzip -l
-U".
I've just checked the description of yours - it seems the case for you, too. So your script is
UTF-8 encoded file with the hardcoded path, it alreaddy add the UTF-8 encoded filename. If your
terminal is UTF-8, and you output a non UTF-8 strings, the best case were to see some abrakadabra,
or nothing. That was the reason i was asking about the terminal charset. Fe do a small experiment,
given your terminal is in UTF-8
php -r '$s0 = "ÑеÑÑ"; $s1 = iconv("UTF-8", "cp1251",
$s0); echo "$s0", " ", strlen($s0), " ", "$s1", "
", strlen($s1), "\n"; '
In my terminal, i only see as out "ÑеÑÑ 8 4" - so it's unable to out the
string $s1 in a single byte charset which is invalid for UTF-8.
Now, when coming back to the zip entries file names, please consider this:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$s0 = $zip->getNameIndex(0);
echo $s0, " ", strlen($s0), "\n";
$zip->close();
echo 'ok';
} else {
echo 'Fehler';
}
This shows me on the UTF-8 terminal "ÑеÑÑ123 11" when i use it on a zip file
created by PHP.
So you see that there is no issue in PHP in creating zips with UTF-8 filenames. Whatever the unzip
commando shows, for PHP it seems to be correct with the case. I have completely no clue which
conversions on filenames the zip utility does, but that seems to be not relevant for the current
case. Furthermore, what PHP does seems to be rather correct in opposite to the regular zip utility.
Thanks.
------------------------------------------------------------------------
[2015-12-06 17:17:49] slavikca at gmail dot com
to compare, here how native linux utility work:
root@php7:/var/www/html# zip tzip.zip ÑеÑÑ1
adding: ÑеÑÑ1 (deflated 38%)
root@php7:/var/www/html# unzip -l -U tzip.zip
Archive: tzip.zip
Length Date Time Name
--------- ---------- ----- ----
16 2015-12-04 18:10 #U0442#U0435#U0441#U04421
So, in that case, I see, that filename is stored in UTF-8.
------------------------------------------------------------------------
[2015-12-06 17:14:41] slavikca at gmail dot com
yea, the reason I see this in terminal:
/var/www/test/web/#U0442#U0435#U0441#U0442123
is because of "-U" option:
-U use escapes for all non-ASCII Unicode
So, your terminal output confirms, that filename in archive file is NOT in UTF-8.
For UTF-8, you would see escaped chars, such as "#U0442#U0435#U0441#U0442123"
------------------------------------------------------------------------
[2015-12-06 16:52:19] ab@php.net
Hmm ... or do I err, and the zip utility is actually converts to pure Unicode? Than could explain
the difference in the output showing, still confusing. Anyway, adding filenames in UTF-8 works and
is not an issue.
Thanks.
------------------------------------------------------------------------
[2015-12-06 16:37:29] ab@php.net
Thanks for the additional info. It seems that it is not a bug in PHP. In 7.0.0, i use
$zip->addFile(__FILE__, "\u{442}\u{435}\u{441}\u{442}123.txt");
which effectively adds a file named ÑеÑÑ123.txt to the zip archive. This is what I see
in my terminal
$ unzip -l -U bug71034.zip
Archive: bug71034.zip
Length Date Time Name
--------- ---------- ----- ----
319 2015-12-06 17:25 ÑеÑÑ123.txt
--------- -------
319 1 file
Same i get with your original piece of code.
Please note, in the description you list to see the following:
/var/www/test/web/#U0442#U0435#U0441#U0442123
That means terminal is not in UTF-8. It is not critical for the PHP behavior, but it avoids any
possible misinterpretation when you list out the results. So were worth it to configure.
so after all, the issue on your side appears to be that the filename you pass to ext/zip is not
UTF-8. Consequently that name is stored into the zip file. It is weird given all your locale is
UTF-8 and the PHP file with the hard coded file name as well, but it is the only explanation for the
behavior. The filename can be forced to UTF-8 when you fe convert it with iconv and pass it as
second argument to ZipArchive::addFile().
Thanks.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=71034
--
Edit this bug report at https://bugs.php.net/bug.php?id=71034&edit=1