Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Zlib Compression Functions : gzread

gzread

Binary-safe gz-file read (PHP 4, PHP 5)
string gzread ( resource zp, int length )

Example 2732. gzread() example

<?php
// get contents of a gz-file into a string
$filename = "/usr/local/something.txt.gz";
$zd = gzopen($filename, "r");
$contents = gzread($zd, 10000);
gzclose($zd);
?>

Code Examples / Notes » gzread

methatron

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.


zaotong

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.


Change Language


Follow Navioo On Twitter
gzclose
gzcompress
gzdecode
gzdeflate
gzencode
gzeof
gzfile
gzgetc
gzgets
gzgetss
gzinflate
gzopen
gzpassthru
gzputs
gzread
gzrewind
gzseek
gztell
gzuncompress
gzwrite
readgzfile
zlib_get_coding_type
eXTReMe Tracker