<?php
error_reporting(-1);
ini_set("track_errors", "1");
function displaysBZIP2File($fn)
{
echo "Reading $fn:\n";
$bz = @bzopen($fn, "r");
if( $bz === FALSE ){
echo "ERROR: bzopen() failed: $php_errormsg\n";
return;
}
$errno = bzerrno($bz);
if( $errno != 0 ){
echo "ERROR: bzopen(): BZIP2 decoding failed: ", bzerrstr($bz), "\n";
@bzclose($bz);
return;
}
while(! feof($bz) ) {
$s = bzread($bz, 100);
if( $s === FALSE ){
echo "ERROR: bzread() failed: $php_errormsg\n";
@bzclose($bz);
return;
}
$errno = bzerrno($bz);
if( $errno != 0 ){
echo "ERROR: bzread(): BZIP2 decoding failed: ", bzerrstr($bz), "\n";
@bzclose($bz);
return;
}
echo "read: ", var_export($s, true), "\n";
}
if( ! bzclose($bz) ){
echo "ERROR: bzclose() failed: $php_errormsg\n";
}
}
$fn = "test.bz2";
file_put_contents($fn, bzcompress("Content of the file."));
displaysBZIP2File($fn); file_put_contents($fn, "This ia plain text file, no compression at all!");
displaysBZIP2File($fn); $plain = str_repeat("Quite random string. ", 1000);
$compressed = bzcompress($plain);
$compressed_corrupted = $compressed;
$compressed_corrupted[(int)(strlen($compressed)/2)] = 'X'; file_put_contents($fn, $compressed_corrupted);
displaysBZIP2File($fn);
?>