arnapou/zip

Library - Zip utility library.

v1.3 2024-04-14 15:59 UTC

This package is auto-updated.

Last update: 2024-04-18 20:18:31 UTC


README

pipeline coverage

This library brings a few utility classes to manage zip files.

Basically, it

  • removes all the falsy behaviour of ZipArchive by throwing exceptions
  • gives safe methods for 95% of your use cases
  • split "read" and "write" into dedicated objects to reduce the risk to do weird things

Installation

composer require arnapou/zip

packagist 👉️ arnapou/zip

Reader

The aim is to provide a readonly manner to read/extract a zip archive with dedicated objects.

$reader = new \Arnapou\Zip\ZipReader($zipFilename);

foreach ($reader as $item) {
    // $item is :
    // - either a \Arnapou\Zip\Reading\Zipped\ZippedDir object
    // - either a \Arnapou\Zip\Reading\Zipped\ZippedFile object
}

// But you can iterate directly of on files. 
foreach ($reader->getFiles() as $zippedFile) {
    // Only files
}

// Or on Dirs. 
foreach ($reader->getDirs() as $zippedDir) {
    // Only dirs
}

Writer

This writer wrapper does only writing.

$writer = new \Arnapou\Zip\ZipWriter($zipFilename);

$writer->addFileContent('some-file.txt', 'Hello World');
$writer->addPath('/some/path/to/zip');
$writer->addFile('/some/file.txt', 'file.txt');

$writer->close();

For huge files, you can stream write the zip by changing the default adapter.

$writer = new \Arnapou\Zip\ZipWriter(
    $zipFilename,
    new \Arnapou\Zip\Writing\Adapter\ZipStreamWriteOnly()
);

$writer->addFile('/some/really/huge/file.txt', 'file.txt');

$writer->close();

Changelog versions

StartTag, BranchPhp
15/01/20241.x, main8.3