Home > Article > Backend Development > PHP compression and archiving—Bzip2
bzip2 function is used to transparently read and write bzip2 (.bz2) compressed files.
This module uses functions in Julian Seward’s » bzip2 library. This module requires bzip2/libbzip2 version >= 1.0.x.
PHP’s Bzip2 support is not turned on by default. The --with-bz2[=DIR] configuration option is required when compiling PHP to activate bzip2 support.
The following example opens a temporary file and writes a test string, and then outputs the contents of the file:
Example #1 bzip2 small example
<?php $filename = "/tmp/testfile.bz2"; $str = "This is a test string.\n"; // 打开一个文件用于写入 $bz = bzopen($filename, "w"); // 写入字符串到文件 bzwrite($bz, $str); // 关闭文件 bzclose($bz); // 打开文件用于读取 $bz = bzopen($filename, "r"); // 读取 10 个字符 echo bzread($bz, 10); // 输出直到文件末尾(或者后面的 1024 个字符),并关闭。 echo bzread($bz); bzclose($bz); ?>
Related functions:
bzclose — close a bzip2 file
bzcompress — Compress a string into bzip2 encoded data
bzdecompress — Decompress bzip2 encoded data
bzerrno — Return a bzip2 error code
bzerror — Return an array containing the bzip2 error number and error string
bzerrstr — Returns a bzip2 error string
bzflush — Forces writing of all write buffer data
bzopen — Opens a bzip2-compressed file
bzread — Binary safe reading of bzip2 files
bzwrite — Binary safe Write bzip2 files