I don't think it would be wise to open a whole gzip file (it _is_ compressed for a reason, and likely to be a very big file) into a single string. You would most likely hit the php memory limits. Instead, if you need to process the file, put your gzread calls in a loop, and read incrementally by a setting $length to a constant value.
gzread
(PHP 4, PHP 5)
gzread — Lecture de fichier compressé binaire
Description
string gzread
( resource $zp
, int $length
)
gzread() lit jusqu'à length octets dans le fichier compressé gzip, représenté par zp . La lecture s'arrête lorsque length octets (décompressés) ont été lus, ou que la fin du fichier a été atteinte.
Liste de paramètres
- zp
-
Le pointeur de fichier gz. Il doit être valide et doit pointer vers un fichier ouvert avec succès grâce à la fonction gzopen().
- length
-
Le nombre d'octets lus.
Valeurs de retour
Les données qui ont été lues.
Exemples
Exemple #1 Exemple avec gzread()
<?php
// récupère le contenu d'un fichier gz dans une chaîne
$filename = "/usr/local/something.txt.gz";
$zd = gzopen($filename, "r");
$contents = gzread($zd, 10000);
gzclose($zd);
?>
gzread
utku
08-Feb-2008 01:31
08-Feb-2008 01:31
zaotong at yahoo dot com
07-Mar-2007 03:06
07-Mar-2007 03:06
As was shown to me in another forum there is a way to get the uncompressed size of the gz file by viewing the last 4 bytes of the gz file.
Here is a piece of code that will do this
<?php
$FileRead = 'SomeText.txt';
$FileOpen = fopen($FileRead, "rb");
fseek($FileOpen, -4, SEEK_END);
$buf = fread($FileOpen, 4);
$GZFileSize = end(unpack("V", $buf));
fclose($FileOpen);
$HandleRead = gzopen($FileRead, "rb");
$ContentRead = gzread($HandleRead, $GZFileSize);
?>
This will read the last 4 bytes of the gz file and use it as the file int for the gzread.
Thanks to stereofrog for helping me with this code.
methatron at hotmail dot com
27-Sep-2006 12:33
27-Sep-2006 12:33
Be aware that gzread's second parameter - length reffers to the file's uncompressed size, therefore using this code:
<?php
$file_name = "/usr/local/something.txt.gz";
if($file_handle = gzopen($file_name, "r"))
{
$contents = gzread($file_handle, filesize($file_name));
gzclose($file_name);
}
?>
will probably truncate the content of the file as filesize checks for the file's compressed size.
So either use the actual uncompressed size, if you know it, or use an aribtrary big enough length, as gzreading will stop at the end of the file anyway.
