PHP Files
Reading and Writing Compressed Files
Problem
You want to read or write compressed files.
Solution
Use the compress.zlib or compress.bzip2 stream wrapper with the standard file functions.
To read data from a gzip-compressed file:
$file = __DIR__ . '/lots-of-data.gz';
$fh = fopen("compress.zlib://$file",'r') or die("can't open: $php_errormsg");
if ($fh) {
while ($line = fgets($fh)) {
// $line is the next line of uncompressed data
}
fclose($fh) or die("can't close: $php_errormsg");
}
Discussion
The compress.zlib stream wrapper provides access to files that have been compressed with the gzip algorithm. The compress.bzip2 stream wrapper provides access to files that have been compressed with the bzip2 algorithm. Both stream wrappers allow reading, writing, and appending with compressed files. To enable the zlib and bzip2 compression streams, build PHP with --with-zlib and --with-bz2, respectively.
In addition to the stream wrappers, which allow access to compressed local files, there are stream filters that compress (or uncompress) arbitrary streams on the fly. The zlib.deflate and zlib.inflate filters compress and uncompress data according to the zlib “deflate” algorithm. The bzip2.compress and bzip2.uncompress filters do the same for the bzip2 algorithm.
Each stream filter must be applied to a stream after it is created. This example uses the bzip2 stream filters to read compressed data from a URL:
$fp = fopen('http://www.example.com/something-compressed.bz2','r');
if ($fp) {
stream_filter_append($fp, 'bzip2.uncompress');
while (! feof($fp)) {
$data = fread($fp);
// do something with $data;
}
fclose($fp);
}
No comments:
Post a Comment