maennchen / zipstream-php
ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.
Fund package maintenance!
maennchen
Installs: 252 502 969
Dependents: 132
Suggesters: 2
Security: 0
Stars: 1 861
Watchers: 20
Forks: 112
Open Issues: 2
pkg:composer/maennchen/zipstream-php
Requires
- php-64bit: ^8.3
- ext-mbstring: *
- ext-zlib: *
Requires (Dev)
- ext-zip: *
- brianium/paratest: ^7.7
- friendsofphp/php-cs-fixer: ^3.16
- guzzlehttp/guzzle: ^7.5
- mikey179/vfsstream: ^1.6
- php-coveralls/php-coveralls: ^2.5
- phpunit/phpunit: ^12.0
- vimeo/psalm: ^6.0
Suggests
- guzzlehttp/psr7: ^2.4
- psr/http-message: ^2.0
- dev-main
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.1-beta.1
- 3.1.0
- 3.1.0-beta.1
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0-beta.2
- 3.0.0-beta.1
- v2.x-dev
- 2.4.0
- 2.3.0
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- v1.x-dev
- 1.2.0
- 1.1.0
- v1.0.0
- v1.0.0-alpha.2
- v1.0.0-alpha.1
- v0.x-dev
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- 0.3.1
- 0.3.0
- 0.2.2
- dev-dependabot/github_actions/github-actions-ff6e0617b3
This package is auto-updated.
Last update: 2025-10-20 06:29:26 UTC
README
Unstable Branch
The main branch is not stable. Please see the
releases for a stable
version.
Overview
A fast and simple streaming zip file downloader for PHP. Using this library will save you from having to write the Zip to disk. You can directly send it to the user, which is much faster. It can work with S3 buckets or any PSR7 Stream.
Please see the LICENSE file for licensing and warranty information.
Installation
Simply add a dependency on maennchen/zipstream-php to your project's
composer.json file if you use Composer to manage the dependencies of your
project. Use following command to add the package to your project's dependencies:
composer require maennchen/zipstream-php
Usage
For detailed instructions, please check the Documentation.
// Autoload the dependencies require 'vendor/autoload.php'; // create a new zipstream object $zip = new ZipStream\ZipStream( outputName: 'example.zip', // enable output of HTTP headers sendHttpHeaders: true, ); // create a file named 'hello.txt' $zip->addFile( fileName: 'hello.txt', data: 'This is the contents of hello.txt', ); // add a file named 'some_image.jpg' from a local file 'path/to/image.jpg' $zip->addFileFromPath( fileName: 'some_image.jpg', path: 'path/to/image.jpg', ); // finish the zip stream $zip->finish();
Callback Output
You can stream ZIP data to a custom callback function instead of directly to the browser:
use ZipStream\ZipStream; use ZipStream\Stream\CallbackStreamWrapper; // Stream to a callback function with proper file handling $outputFile = fopen('output.zip', 'wb'); $backupFile = fopen('backup.zip', 'wb'); $zip = new ZipStream( outputStream: CallbackStreamWrapper::open(function (string $data) use ($outputFile, $backupFile) { // Handle ZIP data as it's generated fwrite($outputFile, $data); // Send to multiple destinations efficiently echo $data; // Browser fwrite($backupFile, $data); // Backup file }), sendHttpHeaders: false, ); $zip->addFile('hello.txt', 'Hello World!'); $zip->finish(); // Clean up resources fclose($outputFile); fclose($backupFile);
Questions
💬 Questions? Please Read This First!
If you have a question about using this library, please do not email the authors directly. Instead, head over to the GitHub Discussions page — your question might already be answered there! Using Discussions helps build a shared knowledge base, so others can also benefit from the answers. If you need dedicated 1:1 support, check out the options available on @maennchen's sponsorship page.
Upgrade to version 3.1.2
- Minimum PHP Version:
8.2
Upgrade to version 3.0.0
General
- Minimum PHP Version:
8.1 - Only 64bit Architecture is supported.
- The class
ZipStream\Option\Methodhas been replaced with the enumZipStream\CompressionMethod. - Most classes have been flagged as
@internaland should not be used from the outside. If you're using internal resources to extend this library, please open an issue so that a clean interface can be added & published. The externally available classes & enums are:ZipStream\CompressionMethodZipStream\Exception*ZipStream\ZipStream
Archive Options
- The class
ZipStream\Option\Archivehas been replaced in favor of named arguments in theZipStream\ZipStreamconstructor. - The archive options
largeFileSize&largeFileMethodhas been removed. If you want differentcompressionMethodsbased on the file size, you'll have to implement this yourself. - The archive option
httpHeaderCallbackchanged the type fromcallabletoClosure. - The archive option
zeroHeaderhas been replaced with the optiondefaultEnableZeroHeaderand can be overridden for every file. Its default value changed fromfalsetotrue. - The archive option
statFileswas removed since the library no longer checks filesizes this way. - The archive option
deflateLevelhas been replaced with the optiondefaultDeflateLeveland can be overridden for every file. - The first argument (
name) of theZipStream\ZipStreamconstructor has been replaced with the named argumentoutputName. - Headers are now also sent if the
outputNameis empty. If you do not want to automatically send http headers, setsendHttpHeaderstofalse.
File Options
- The class
ZipStream\Option\Filehas been replaced in favor of named arguments in theZipStream\ZipStream->addFile*functions. - The file option
methodhas been renamed tocompressionMethod. - The file option
timehas been renamed tolastModificationDateTime. - The file option
sizehas been renamed tomaxSize.
Upgrade to version 2.0.0
https://github.com/maennchen/ZipStream-PHP/tree/2.0.0#upgrade-to-version-200
Upgrade to version 1.0.0
https://github.com/maennchen/ZipStream-PHP/tree/2.0.0#upgrade-to-version-100
Contributing
ZipStream-PHP is a collaborative project. Please take a look at the .github/CONTRIBUTING.md file.
Version Support
Versions are supported according to the table below.
Please do not open any pull requests contradicting the current version support status.
Careful: Always check the README on main for up-to-date information.
| Version | New Features | Bugfixes | Security |
|---|---|---|---|
| 3 | ✓ | ✓ | ✓ |
| 2 | ✗ | ✗ | ✓ |
| 1 | ✗ | ✗ | ✗ |
| 0 | ✗ | ✗ | ✗ |
This library aligns itself with the PHP core support. New features and bugfixes will only target PHP versions according to their current status.
See: https://www.php.net/supported-versions.php
About the Authors
- Paul Duncan pabs@pablotron.org - https://pablotron.org/
- Jonatan Männchen jonatan@maennchen.ch - https://maennchen.dev
- Jesse G. Donat donatj@gmail.com - https://donatstudios.com
- Nicolas CARPi nico-git@deltablot.email - https://www.deltablot.com
- Nik Barham nik@brokencube.co.uk - https://www.brokencube.co.uk