php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81222 ZipArchive Deletes 0-length file
Submitted: 2021-07-04 06:45 UTC Modified: 2021-07-05 11:57 UTC
From: eclipsechasers2 at yahoo dot com Assigned: cmb (profile)
Status: Wont fix Package: Filesystem function related
PHP Version: 7.4.21 OS: Windows 10
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-07-04 06:45 UTC] eclipsechasers2 at yahoo dot com
Description:
------------
On Windows, when a zero-length file is opened as a ZipArchive, the file is deleted when the zip is closed. This seems to happen in all releases of PHP. It does not seem to happen on Linux systems.

Test script:
---------------
<?php
$filename = 'zerolengthfile.zip';
file_put_contents($filename, '');
assert(file_exists($filename));
$zip = new ZipArchive();
$zip->open($filename);
$zip->close();
assert(file_exists($filename));

Expected result:
----------------
I expect the assert at the end to not issue a message. I also expect zerolengthfile.zip to exist.

Actual result:
--------------
Warning: assert(): assert(file_exists($filename)) failed in C:\phpexceltest\Owen
\testfile2.php on line 8

zerolengthfile.zip does not exist

If the $zip->close() statement is removed, the assert at the end does not issue a message. Nevertheless, zerolengthfile.zip does not exist when the script terminates.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-05 01:05 UTC] eclipsechasers2 at yahoo dot com
It looks like the problem can occur with non-zero-length files as well, though not as predictably.
 [2021-07-05 11:57 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-07-05 11:57 UTC] cmb@php.net
First, this is not related to the OS, but rather to the libzip
version in use (an empty file is deleted as of libzip 1.0.0).

Anyhow, this is caused by trying to keep compatibility with libzip
< 1.6.0, which does not accept empty files as valid zip archives
any longer, what had been reported as bug #79296, and mitigated:
<https://bugs.php.net/79296#1584700784>.

The behavior regarding empty archives which are not updated is
inconvenient, but you can set the ZipArchive::RDONLY flag if
your're not planning to update the archive, in which case the
archive will not be opened in the first place
(ZipArchive::ER_NOZIP).
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 16:03:36 2021 UTC