<?php
error_reporting(-1);
ini_set("track_errors", "1");
function displaysBZIP2File($fn)
{
echo "正在读取 $fn:\n";
$bz = @bzopen($fn, "r");
if( $bz === FALSE ){
echo "错误:bzopen() 失败: $php_errormsg\n";
return;
}
$errno = bzerrno($bz);
if( $errno != 0 ){
echo "错误:bzopen():BZIP2 解码失败: ", bzerrstr($bz), "\n";
@bzclose($bz);
return;
}
while(! feof($bz) ) {
$s = bzread($bz, 100);
if( $s === FALSE ){
echo "错误:bzread() 失败: $php_errormsg\n";
@bzclose($bz);
return;
}
$errno = bzerrno($bz);
if( $errno != 0 ){
echo "错误:bzread():BZIP2 解码失败: ", bzerrstr($bz), "\n";
@bzclose($bz);
return;
}
echo "读取: ", var_export($s, true), "\n";
}
if( ! bzclose($bz) ){
echo "错误:bzclose() 失败: $php_errormsg\n";
}
}
$fn = "test.bz2";
file_put_contents($fn, bzcompress("文件内容。"));
displaysBZIP2File($fn); file_put_contents($fn, "这是一个纯文本文件,没有压缩!");
displaysBZIP2File($fn); $plain = str_repeat("非常随机的字符串。 ", 1000);
$compressed = bzcompress($plain);
$compressed_corrupted = $compressed;
$compressed_corrupted[(int)(strlen($compressed)/2)] = 'X'; file_put_contents($fn, $compressed_corrupted);
displaysBZIP2File($fn);
?>