--- date: "2007-09-22T05:40:41Z" title: Streaming Zip Files with PHP ---

The problem: how do you dynamically generate arbitrarily large downloadable zip files from PHP? All of the existing solutions I found all generate a local temp file, which means the server needs to

I wasn't particularly fond of either constraint, so I came up with a solution: ZipStream-PHP. ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server. Using it is dirt simple, too. Here's how:

# create a new stream object
$zip = new ZipStream('example.zip');

# then add one or more files

# add first file
$data = file_get_contents('some_file.gif');
$zip->add_file('some_file.gif', $data);

# add second file
$data = file_get_contents('another_file.txt');
$zip->add_file('another_file.txt', $data);

# finally, finish the stream
$zip->finish();

You can also set file comments and creation dates, like so:

$data = file_get_contents('foo.txt');
$zip->add_file('foo.txt', $data, array(
  'comment' => 'this is an interesting comment',
  'time'    => time() - 3600, # created one hour ago
));

Here are the links, enjoy: