arnapou / zip
Library - Zip utility library.
v1.3
2024-04-14 15:59 UTC
Requires
- php: ~8.3.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: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.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.3 |
---|---|---|
15/01/2024 | 1.x, main | × |