gzopen("php://output","wb") doesn't work on a web server, nor does fopen("compress.zlib://php://output","wb").
Here is a snippet to gzip a file and output it on the fly, without using a temporary file, without reading the file into memory, and without reading the file more than once:
<?php
$fin = fopen($file, "rb");
if ($fin !== FALSE) {
$fout = fopen("php://output", "wb");
if ($fout !== FALSE) {
fwrite($fout, "\x1F\x8B\x08\x08".pack("V", filemtime($file))."\0\xFF", 10);
$oname = str_replace("\0", "", basename($file));
fwrite($fout, $oname."\0", 1+strlen($oname));
$fltr = stream_filter_append($fout, "zlib.deflate", STREAM_FILTER_WRITE, -1);
$hctx = hash_init("crc32b");
if (!ini_get("safe_mode")) set_time_limit(0);
$con = TRUE;
$fsize = 0;
while (($con !== FALSE) && !feof($fin)) {
$con = fread($fin, 64 * 1024);
if ($con !== FALSE) {
hash_update($hctx, $con);
$clen = strlen($con);
$fsize += $clen;
fwrite($fout, $con, $clen);
}
}
stream_filter_remove($fltr);
$crc = hash_final($hctx, TRUE);
fwrite($fout, $crc[3].$crc[2].$crc[1].$crc[0], 4);
fwrite($fout, pack("V", $fsize), 4);
fclose($fout);
}
fclose($fin);
}
?>