Ne-Lexa / php-zip

PhpZip is a php-library for extended work with ZIP-archives.
MIT License
491 stars 60 forks source link

zip open throws exception when reading a zip file in a loop. #96

Open saisubodhMSFT opened 1 year ago

saisubodhMSFT commented 1 year ago
Q A
Library version(s): 4.0.1
PHP version(s): 8.2
OS (with bit depth): Alpine Linux 3.16

Description
In the following example code, the program throws an exception at $zip->openFiile() after 2nd iteration of the for loop.

How to reproduce

function testZip() { // path to empty zip file $zipFilePath = 'path/to/zipfile';

    for ($i=0; $i<20; $i++) {
            $zip = new \PhpZip\ZipFile();
            if ($zip->openFile($zipFilePath)) {
                    $fileContents = file_get_contents('example.txt');
                    $zip->addFromString('example' . strval($i) . '.txt', $fileContents);
                    $zip->saveAsFile($zipFilePath);
                    $zip->close();
            }
    }        

}

Additional context
Error Message: Exception: example0.txt (expected Local File Header)

Stack Trace:

File: ../vendor/nelexa/zip/src/IO/ZipReader.php Line: 615 Trace:

0 /home/site/wwwroot/wp-content/plugins/azure_app_service_migration/vendor/nelexa/zip/src/IO/ZipReader.php(515): PhpZip\IO\ZipReader->loadLocalExtraFields(Object(PhpZip\Model\ZipEntry))

1 /home/site/wwwroot/wp-content/plugins/azure_app_service_migration/vendor/nelexa/zip/src/IO/ZipReader.php(363): PhpZip\IO\ZipReader->readZipEntry(Resource id #69)

2 /home/site/wwwroot/wp-content/plugins/azure_app_service_migration/vendor/nelexa/zip/src/IO/ZipReader.php(117): PhpZip\IO\ZipReader->readCentralDirectory(Object(PhpZip\Model\EndOfCentralDirectory))

3 /home/site/wwwroot/wp-content/plugins/azure_app_service_migration/vendor/nelexa/zip/src/ZipFile.php(152): PhpZip\IO\ZipReader->read()

4 /home/site/wwwroot/wp-content/plugins/azure_app_service_migration/vendor/nelexa/zip/src/ZipFile.php(113): PhpZip\ZipFile->openFromStream(Resource id #68, Array)

odan commented 1 year ago

I have tried to reproduce this problem on my computer and can say that this is not the case.

The error message indicates that your "original" ZIP file may be "corrupted".