bzread
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
bzread — bzip2 文件二进制安全地读取
说明
bzread(resource
$bz
, int $length
= 1024): string|falsebzread() 从指定的 bzip2 文件指针中读取数据。
读取到 length
(未经压缩的长度)个字节,或者到文件尾,取决于先到哪个。
参数
-
bz
-
文件指针。它必须是有效的并且指向 bzopen() 成功打开的文件。
-
length
-
如果没有提供, bzread() 一次会读入 1024 个字节(未经压缩的长度)。 一次最大可读入 8192 个未压缩的字节。
返回值
返回解压的数据,在错误时返回 false
。
范例
示例 #1 bzread() 范例
<?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;
?>

User Contributed Notes 2 notes
user@anonymous ¶
10 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);
Anonymous ¶
6 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);