arnapou / zip
Library - Zip utility library.
v1.4.1
2024-12-11 12:34 UTC
Requires
- php: ~8.3.0 || ~8.4.0
- ext-mbstring: *
- ext-zip: *
- ext-zlib: *
- arnapou/stream: ^1.3
Requires (Dev)
- ext-redis: *
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
- psr/http-message: ^2.0
README
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();
Php versions
Date | Ref | 8.4 | 8.3 |
---|---|---|---|
25/11/2024 | 1.4.x, main | × | × |
15/01/2024 | 1.0 - 1.3 | × |