|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-02-26 23:16 UTC] lists dot ban at herbesfolles dot org
Description:
------------
When adding a file to a Zip archive, the user expects to be able to unlink the original file without issues. The documentation on ZipArchive::addFile() even has a (admittedly strangely worded) note suggesting it would work:
"When a file is set to be added to the archive, PHP will attempt to lock the file and it is only released once the ZIP operation is done. In short, it means you can first delete an added file after the archive is closed."
However, in practice it doesn't work, and depending on the PHP version (or build?) it could even silently fail. PHP 5.5.9 from Debian Sid fails on ZipArchive::close() with StatusString as "No error"; PHP 5.7.0-dev from today Git don't even report any problem. In any case, the result zip is not created.
This is both with system (0.11.2) and bundled libzip.
Test script:
---------------
$zip = new ZipArchive();
$zip->open('output.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
file_put_contents('dummy.txt', 'hello world');
if (! $zip->addFile('dummy.txt'))
echo "addFile() failed\n";
unlink('dummy.txt');
if (! $zip->close())
echo "close() failed\n";
echo "output.zip size: ", filesize('output.zip'), "\n";
@unlink('output.zip');
Expected result:
----------------
output.zip size: 129
Actual result:
--------------
output.zip size:
Warning: filesize(): stat failed for output.zip in zipbug.php on line 9
Patches0001-Zip-fix-creating-archive-if-an-added-file-is-removed.patch (last revision 2014-02-26 23:18 UTC by lists dot ban at herbesfolles dot org)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 16 23:00:01 2025 UTC |
> It is not really a bug but how libzip works. OK, fair enough. > I did not hear about a change in this area but when this change happens, the doc will be updated accordingly. As I said earlier, I believe the documentation should be updated to be more clear in any case. Currently, even native English speakers agree that the second sentence ("In short, it means you can first delete an added file after the archive is closed") is at best confusing, and I for myself (non-native speaker) could simply not understand anything in this sentence. I propose to e.g. simply replace "first" with "only": "In short, it means you can only delete an added file after the archive is closed." This way, I would have understood, and AFAICT it's only clearer for everyone. Though, I read the previous sentence ("When a file is set to be added to the archive, PHP will attempt to lock the file and it is only released once the ZIP operation is done.") and its "PHP will attempt to lock the file" as that at worse unlink() would fail if I call it before the zip operation is done. So maybe the part about "PHP will attempt to lock the file" should be removed since in practice it's not the case (or it doesn't work under Linux). --- Also, again, the test case in the patch (even without the patch) show a regression in Git where if the file was unlinked before save() and after addFile() *no error* would be reported by the API. This is a bug in any case, and 5.5.9 used to report a problem on close(). Should I create new reports for each of these issues? One against the documentation and the other again against the Zip extension for that regression?