maennchen/zipstream-php
Stream ZIP files on the fly in PHP without temporary files. zipstream-php lets you send large, dynamically generated archives directly to the browser with low memory usage, supporting modern ZIP features and efficient download/response streaming.
Install via Composer: composer require maennchen/zipstream-php. Begin by instantiating ZipStream\ZipStream with an output stream (e.g., fopen('php://output', 'w')), then add files using addFile() or addFileFromPath(). For immediate browser download, set proper headers before streaming:
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="archive.zip"');
$zip = new ZipStream\ZipStream(fopen('php://output', 'wb'));
$zip->addFile('readme.txt', 'Hello, world!');
$zip->finish();
The earliest success case: generating a ZIP on-the-fly without writing to disk.
addFile() with custom filenames, and optional ZipStream\Option\Method::DEFLATE() for compression.StreamedResponse for clean controller usage:
$response = new StreamedResponse(function () use ($zip) {
$zip->addFile('report.csv', $csvContent);
$zip->finish();
});
$response->headers->set('Content-Type', 'application/zip');
return $response;
addDirectory('folder/'), preserving internal path structure.addFileFromPath() for large files to avoid loading them into memory.output_buffering in php.ini or use while (ob_get_level()) ob_end_flush();—ZIP streaming fails silently with buffering active.header() before $zip->finish()—any output (including whitespace) prior causes corruption or "headers already sent" errors.ZipStream\Option\FileName::UTF8() to ensure non-ASCII filenames render correctly.ZipStream\Option\Method::STORE() (no compression) reduces CPU overhead for large binary files.$zip->finish() in try/catch—exceptions may surface late (e.g., disk full during final write).fopen('php://memory', 'c+') for unit tests without side effects.ZipStream\Option\GlobalOption::enableZip64(false) cautiously).How can I help you explore Laravel packages today?