bzip2 压缩包操作类

属性
命名空间 fize\misc
类名 Bz2
方法:
方法名 说明
__construct() 构造
__destruct() 析构函数
close() 关闭 bzip2 文件
compress() 把一个字符串压缩成 bzip2 编码数据
decompress() 解压经 bzip2 编码过的数据
errno() 返回一个 bzip2 错误码
error() 返回包含 bzip2 错误号和错误字符串的一个数组
errstr() 返回一个 bzip2 的错误字符串
flush() 强制写入所有写缓冲区的数据
open() 打开一个经 bzip2 压缩过的文件
read() 从文件读取数据
write() 二进制安全地写入 bzip2 文件

方法

__construct()

构造

public function __construct (

    string $file,

    string $mode

)
参数:
名称 说明
file 待打开的文件的文件名,或者已经存在的资源流。
mode 模式
参数 `$file` :
  指定文件不存在时将尝试创建
参数 `$mode` :
   fopen() 函数类似,但仅仅支持 'r'(读)和 'w'(写)。

__destruct()

析构函数

public function __destruct ()

close()

关闭 bzip2 文件

public function close () : bool

compress()

把一个字符串压缩成 bzip2 编码数据

public static function compress (

    string $source,

    int $blocksize = 4,

    int $workfactor = 0

) : string
参数:
名称 说明
source 待压缩的字符串。
blocksize 指定压缩时使用的块大小
workfactor 控制压缩阶段出现最坏的重复性高的情况下输入数据时的行为
返回值:

压缩后的字符串

参数 `$blocksize` :
  应该是一个 1-9 的数字。9 可以有最高的压缩比,但会使用更多的资源。
参数 `$workfactor` :
  值可以是在 0  250 之间,0 是一个特殊的情况。

decompress()

解压经 bzip2 编码过的数据

public static function decompress (

    string $source,

    int $small = 0

) : string
参数:
名称 说明
source 编码过的数据
small 是否使用一种内存开销更小的替代算法
返回值:

解压后的字符串

errno()

返回一个 bzip2 错误码

public function errno () : int

error()

返回包含 bzip2 错误号和错误字符串的一个数组

public function error () : array

errstr()

返回一个 bzip2 的错误字符串

public function errstr () : string

flush()

强制写入所有写缓冲区的数据

public function flush () : bool
返回值:成功时返回 TRUE, 或者在失败时返回 FALSE。

open()

打开一个经 bzip2 压缩过的文件

public function open (

    string $file,

    string $mode

)
参数:
名称 说明
file 待打开的文件
mode 模式
参数 `$mode` :
 `fopen()` 函数类似,但仅仅支持 'r'(读)和 'w'(写)。
其他任何模式都会导致 bzopen 返回 FALSE

read()

从文件读取数据

public function read (

    int $length = 1024

) : string
参数:
名称 说明
length 读取字节长度
返回值:

返回解压的数据

参数 `$length` :
读取到 length(未经压缩的长度)个字节,或者到文件尾,取决于先到哪个。
如果没有提供该参数, read()  方法一次会读入 1024 个字节(未经压缩的长度)。
一次最大可读入 8192 个未压缩的字节。

write()

二进制安全地写入 bzip2 文件

public function write (

    string $data,

    int $length = null

) : int
参数:
名称 说明
data 要写入的数据
length 写入字节长度
返回值:

返回写入的数据字节数

注意不能多次调用该方法,bz2文件是一次性写入并覆盖的
参数 `$length` :
  如果提供了参数 `$length` ,将仅仅写入 length(未压缩)个字节,
   data 小于该指定的长度则写入全部数据。