Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$title is deprecated in /home/www/dev/work/website/lvesu/class/controller/blog/php.php on line 28

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/php.manual.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
PHP - Manual: bzread - 互联网笔记

略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: bzread

2025-04-28

bzread

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

bzreadbzip2 文件二进制安全地读取

说明

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

bzread() 从指定的 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;

?>

参见

  • bzwrite() - 二进制安全地写入 bzip2 文件
  • feof() - 测试文件指针是否到了文件结束的位置
  • bzopen() - 打开 bzip2 压缩文件
添加备注

用户贡献的备注 2 notes

up
2
user@anonymous
12 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);

官方地址:https://www.php.net/manual/en/function.bzread.php

冷却塔厂家 广告
中文GPT4.0无需注册 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3