Skip to content

Repository files navigation

ArchiveStream Message Body (PSR-7)

Stream a ZIP file (memory efficient) as a PSR-7 message.

workflow code check

Installation

Use composer to add the package to your dependencies. Supports every PHP version that is receiving security updates.

composer require genkgo/archive-stream

For PHP 7.3, use version 3.1.x or lower.

composer require genkgo/archive-stream@3.0.3

Getting Started

<?phpuseGenkgo\ArchiveStream\Archive;
useGenkgo\ArchiveStream\CallbackContents;
useGenkgo\ArchiveStream\CallbackStringContent;
useGenkgo\ArchiveStream\EmptyDirectory;
useGenkgo\ArchiveStream\FileContent;
useGenkgo\ArchiveStream\Psr7Stream;
useGenkgo\ArchiveStream\StringContent;
useGenkgo\ArchiveStream\TarGzReader;
useGenkgo\ArchiveStream\TarReader;
useGenkgo\ArchiveStream\ZipReader;
$archive = (newArchive())
->withContent(newCallbackStringContent('callback.txt', function () {
return'data';
}))
->withContent(newStringContent('string.txt', 'data'))
->withContent(newFileContent('file.txt', 'local/file/name.txt'))
->withContent(newEmptyDirectory('directory'))
->withContents([newStringContent('string2.txt', 'data')])
->withContents(newCallbackContents(fn () => yieldnewStringContent('string3.txt', 'data')));
$response = $response->withBody(
newPsr7Stream(newZipReader($archive))
);
// or for tar files$response = $response->withBody(
newPsr7Stream(newTarReader($archive))
);
// or for tar.gz files$response = $response->withBody(
newPsr7Stream(newTarGzReader(newTarReader($archive)))
);

Usage in Symfony HttpFoundation (Symfony and Laravel)

useSymfony\Component\HttpFoundation\StreamedResponse;
$stream = newPsr7Stream(newZipReader($archive));
$response = newStreamedResponse(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',
]);

Requirements

  • PHP version actively receiving security updates
  • gmp extension
  • psr/http-message

Limitations

  • Only the Zip64 (version 4.5 of the Zip specification) format is supported.
  • Files cannot be resumed if a download fails before finishing.

Contributors

  • Paul Duncan - Original author
  • Daniel Bergey
  • Andy Blyler
  • Tony Blyler
  • Andrew Borek
  • Rafael Corral
  • John Maguire
  • Frederik Bosch

License

Original work Copyright 2007-2009 Paul Duncan pabs@pablotron.org Modified work Copyright 2013-2015 Barracuda Networks, Inc. Modified work Copyright 2016 Genkgo BV.

Licensed under the MIT License

About

Archive streaming library for PSR-7

Resources

Stars

30 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages