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) {
// write gzip header
fwrite($fout, "\x1F\x8B\x08\x08".pack("V", filemtime($file))."\0\xFF", 10);
// write the original file name
$oname = str_replace("\0", "", basename($file));
fwrite($fout, $oname."\0", 1+strlen($oname));
// add the deflate filter using default compression level
$fltr = stream_filter_append($fout, "zlib.deflate", STREAM_FILTER_WRITE, -1);
// set up the CRC32 hashing context
$hctx = hash_init("crc32b");
// turn off the time limit
if (!ini_get("safe_mode")) set_time_limit(0);
$con = TRUE;
$fsize = 0;
while (($con !== FALSE) && !feof($fin)) {
// deflate works best with buffers >32K
$con = fread($fin, 64 * 1024);
if ($con !== FALSE) {
hash_update($hctx, $con);
$clen = strlen($con);
$fsize += $clen;
fwrite($fout, $con, $clen);
}
}
// remove the deflate filter
stream_filter_remove($fltr);
// write the CRC32 value
// hash_final is a string, not an integer
$crc = hash_final($hctx, TRUE);
// need to reverse the hash_final string so it's little endian
fwrite($fout, $crc[3].$crc[2].$crc[1].$crc[0], 4);
// write the original uncompressed file size
fwrite($fout, pack("V", $fsize), 4);
fclose($fout);
}
fclose($fin);
}
?>
gzopen
(PHP 4, PHP 5)
gzopen — Ouvre un fichier compressé avec gzip
Description
$filename
, string $mode
[, int $use_include_path = 0
] )gzopen() ouvre un fichier compressé avec gzip pour y écrire ou y lire des données.
gzopen() peut être utilisée pour lire un fichier qui n'est pas dans un format gzip ; dans ce cas, gzread() lira directement le fichier sans décompression.
Liste de paramètres
-
filename -
Le nom du fichier.
-
mode -
Comme dans la fonction fopen() (rb ou wb) mais peut également inclure un degré de compression (wb9) ou une stratégie : f pour les données filtrées comme wb6f, h pour Huffman only compression comme wb1h. (Lisez la description de deflateInit2 dans le fichier zlib.h pour plus d'informations sur la stratégie des paramètres.)
-
use_include_path -
Vous pouvez définir ce paramètre optionnel à 1, si vous voulez chercher un fichier également dans l'include_path.
Valeurs de retour
Retourne un pointeur de fichier vers le fichier ouvert, ainsi, la lecture depuis ce pointeur de fichier sera des données décompressées et ce que vous y écrirez, sera compressé.
Si l'ouverture échoue, cette fonction retourne FALSE.
Exemples
Exemple #1 Exemple avec gzopen()
<?php
$fp = gzopen("/tmp/file.gz", "r");
?>
This worked unstable for me under high load (50+ files per second):
<?php
$gz = gzopen ( $file, 'w9' );
gzwrite ( $gz, $content );
gzclose ( $gz );
?>
The following works fine:
<?php
$f = fopen ( $file, 'w' );
fwrite ( $f, gzcompress ( $content, 9 ) );
fclose ( $f );
?>
WARNING gzopen and gzread have a major disadvantage. They makes NO checksum and NO length verification of the gzipped data and discard this valuable information. This should be documented here.
dtorop932 at hotmail dot com's comments, according to my tests, is incorrect. That code wishes to download the entire file before parsing, which is inconvinient. The wget method works though.
Be aware that when opening a remote file on a http server the gzopen will return by default false after 120 seconds waiting to any answer.
RE dubious's comment: "Being able to read gzip streams from ftp and http is near the top of my personal wishlist at the moment..."
One way to read a gzip stream over http is to daisychain stream wrappers, e.g.:
<?
$fp = fopen("compress.zlib://http://some.website.org/example.gz", "r");
?>
"On the fly" gunzipping actually DOES seem to work - it just appears that only LOCAL streams/files (including php://stdin) can be accessed for some reason. I THINK (but have not yet tested) that you could similarly gzopen "php://stdout" and pass a stream of gzipped data to the browser (when run from a web page) or console (when run standalone) through there.
I HAVE tested scripts from the command line like:
wget -q -O- ftp://some.host.net/pub/some_gzip_file.gz | php gunzip_stuff.php
where gunzip_stuff.php would be a script that gzopened "php://stdin" and did gzgets from that stream, and it seems to work fine, but that obviously doesn't help someone wanting to grab gzipped streams from remote sites from a web-based script.
Being able to read gzip streams from ftp and http is near the top of my personal wishlist at the moment...
