Compdigitec Labs

« | Home | »

Simple way to read bzip2 compressed files in PHP

By admin | August 9, 2008

If you are looking for a way to read quick way to read a compressed bzip2 file’s contents, look no further. We have made a function that works similar to the way file_get_contents works.

The code is released under the GPL v2 or later license.

* Reads an uncompressed file from a bzip2 file
* @function file_bzip2_contents
* @author Compdigitec -
* @copyright (C)Copyright 2008 Compdigitec. All rights reserved.
* @license
* @param string $filename The filename of the bz2 compressed file
* @param resource $res An existing resource opened by bzopen
* @return string The contents of the bzip2 file

function file_bzip2_contents($filename,$res = null) {
$file = strval($filename); // Avoid bad input
if($res === null) {
$res = bzopen($filename,'r');
$a = '1qza';
$contents = bzread($res);
if($a == "1qza") {
return $contents;


If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: PHP | No Comments »