pablotron
--
 
ZipStream-PHP
Sat Sep 22 05:39:24 2007 :: Link

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

  • have a web-writable directory large enough to store the intermediate temp file, and
  • be able to generate and start streaming the entire file before the client times out

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 (dynamically generated)
$data = "This is the contents of a dynamically generated text file.";
$zip->add_file('some_file.txt', $data);

# add second file (from existing file)
$zip->add_file_from_path('some_file.gif', 'path/to/some_file.gif');

# 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:

Latest Version:
Older Versions: