©
This document uses PHP Chinese website manual Release
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
bzread — bzip2 文件二进制安全地读取
$bz
[, int $length
= 1024
] )bzread() 从指定的 bzip2 文件指针中读取数据。
读取到 length
(未经压缩的长度)个字节,或者到文件尾,取决于先到哪个。
bz
文件指针。它必须是有效的并且指向 bzopen() 成功打开的文件。
length
如果没有提供, bzread() 一次会读入 1024 个字节(未经压缩的长度)。 一次最大可读入 8192 个未压缩的字节。
返回解压的数据,在错误时返回 FALSE
。
Example #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 ;
?>
[#1] Anonymous [2015-11-08 09:52:16]
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);
[#2] user@anonymous [2012-04-15 19:10:17]
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);