Skip to content

A fast and memory efficient ZIP library for PHP 7

License

Notifications You must be signed in to change notification settings

jdwil/zip-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zip Stream

This library is for generating large zip files with a low memory footprint. The contents of the zip file are never stored in memory at once. Everything is written using streams. This library is for writing zip files only and has no reading capabilities.

License

MIT License

Getting Started

The only requirements are PHP 7.0+ and the zlib extension (almost always enabled).

Installing

composer require jdwil/zip-stream

Running tests

./vendor/bin/phpspec run

Examples

Basic Usage

$zipStream = ZipStream::forFile('/path/to/file.zip');

// Add a file from disk
$zipStream->addFileFromDisk('foo.txt', '/path/to/foo.txt');

// Add a file from a stream
$stream = ReadStream::forFile('/path/to/bar.txt');
$zipStream->addFileFromStream('bar.txt', $stream);

// Add arbirary data
$zipStream->addFile('baz.txt', 'some arbitrary text');

// Always close the Zip Stream
$zipStream->close();

Dealing with huge data sets

$zipStream = ZipStream::forFile('/path/to/file.zip');
$zipStream->beginFile('foo.txt');
while ($data = $somePdoStatement->fetch()) {
  $zipStream->addFilePart(implode(',', $data));
}
$zipStream->endFile();
$zipStream->close();

Stream a ZIP file directly to the user

The file is sent as it is being built, so the download begins immediately for the user.

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="foo.zip"');
header('Content-Transfer-Encoding: binary');

$zipStream = ZipStream::forFile('php://output');
// Build your zip file
$zipStream->close();

Authors

JD Williams me@jdwilliams.xyz

About

A fast and memory efficient ZIP library for PHP 7

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages