ArchiveStream Message Body provides a memory efficient package for streaming Zip files as PSR-7 message. The package require PHP >=5.6.0, gmp extension, and psr/http-message. The only limitation is that only the Zip64 (version 4.5 of the Zip specification) format is supported and files cannot be resumed if a download fails before finishing.

To use it in Symfony HttpFoundation :

use Symfony\Component\HttpFoundation\StreamedResponse;

$stream = new Psr7Stream(new ZipReader($archive));

$response = new StreamedResponse(function () use ($stream) {
    while ($stream->eof() === false) {
        echo $stream->read($blockSize = 1048576);
    }
}, 200, [
    'Content-type' => 'application/zip',
    'Content-Disposition' => 'attachment; filename="file.zip"',
    'Content-Transfer-Encoding' => 'binary',
]);

You can use composer to add the package archive-stream to your dependencies :

composer require genkgo/archive-stream

More information at github, released under an MIT License.

LEAVE A REPLY

Please enter your comment!
Please enter your name here