bzread

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

bzread바이너리 안전 bzip2 파일 읽기

설명

string bzread ( resource $bz [, int $length = 1024 ] )

bzread()는 주어진 bzip2 파일 포인터에서 읽습니다.

(압축 해제된) length 바이트를 읽었거나 EOF에 도달했을 때 읽기를 중단합니다.

인수

bz

bzopen()으로 성공적으로 연 파일을 가리키는 유효한 파일 포인터.

length

지정하지 않으면, bzread()는 한 번에 (압축 해제된) 1024 바이트를 읽습니다.

반환값

압축해제된 데이터를 반환하거나, 오류 시엔 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($bz4096);
}
bzclose($bz);

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

?>

참고

  • bzwrite() - 바이너리 안전 bzip2 파일 쓰기
  • feof() - Tests for end-of-file on a file pointer
  • bzopen() - bzip2 압축 파일 열기

add a note add a note

User Contributed Notes 2 notes

up
4
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
8 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