PHP 8.5.0 RC 2 available for testing

bzread

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

bzread โ€” Binary safe bzip2 file read

ะžะฟะธั

bzread(resource $bz, int $length = 1024): string|false

bzread() reads from the given bzip2 file pointer.

Reading stops when length (uncompressed) bytes have been read or EOF is reached, whichever comes first.

ะŸะฐั€ะฐะผะตั‚ั€ะธ

bz

The file pointer. It must be valid and must point to a file successfully opened by bzopen().

length

If not specified, bzread() will read 1024 (uncompressed) bytes at a time. A maximum of 8192 uncompressed bytes will be read at a time.

ะ—ะฝะฐั‡ะตะฝะฝั, ั‰ะพ ะฟะพะฒะตั€ั‚ะฐัŽั‚ัŒัั

Returns the uncompressed data, or false on error.

ะŸั€ะธะบะปะฐะดะธ

ะŸั€ะธะบะปะฐะด #1 bzread() example

<?php

$file
= "/tmp/foo.bz2";
$bz = bzopen($file, "r") or die("Couldn't open $file");

$decompressed_file = '';
while (!
feof($bz)) {
$decompressed_file .= bzread($bz, 4096);
}
bzclose($bz);

echo
"The contents of $file are: <br />\n";
echo
$decompressed_file;

?>

ะŸั€ะพะณะปัะฝัŒั‚ะต ั‚ะฐะบะพะถ

  • bzwrite() - Binary safe bzip2 file write
  • feof() - Tests for end-of-file on a file pointer
  • bzopen() - Opens a bzip2 compressed file

๏ผ‹add a note

User Contributed Notes 2 notes

up
2
user@anonymous ยถ
13 years ago
Make sure you check for bzerror while looping through a bzfile. bzread will not detect a compression error and can continue forever even at the cost of 100% cpu.

$fh = bzopen('file.bz2','r');
while(!feof($fh)) {
$buffer = bzread($fh);
if($buffer === FALSE) die('Read problem');
if(bzerror($fh) !== 0) die('Compression Problem');
}
bzclose($fh);
up
1
Anonymous ยถ
9 years ago
The earlier posted code has a small bug in it: it uses bzerror instead of bzerrno. Should be like this:

$fh = bzopen('file.bz2','r');
while(!feof($fh)) {
$buffer = bzread($fh);
if($buffer === FALSE) die('Read problem');
if(bzerrno($fh) !== 0) die('Compression Problem');
}
bzclose($fh);
To Top