Compressing files with ZIP on the fly
What can be done with 6 lines of code?

$dir = 'files_you/want_to_compress';
chdir($dir);
$fp = popen('zip -0 -r - .', 'r');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename=" ' .basename($dir) . '.zip"');
fpassthru($fp);
We are basically running command-line ZIP program na sending it's output straight to the browser. ZIP parameters used in this example:
-0 Uses 'store' method which means files are not compressed. Saves a lot of CPU
-r Recursion- compresses all the files in the subdirectories.
Zip is available in Linux and OSX repositories so if you don't have it you can install it easily. In case you use Debian you can just type:
apt-get install zip
And if you prefer Windows you can download Zip 3.0 here:
or just browse their FTP.
Comments