9bzip2 Module

9.1Overview

The bzip2 module provices measures to read/write BZIP2 files. To utilize it, import the bzip2 module using import function.

Below is an example to read data from a BZIP2 file and write its uncompressed data to another file.

import(bzip2)
bzip2.reader('foo.dat.bz2').copyto('foo.dat')

Below is an example to read data from a file and write its compressed data to a BZIP2 file.

import(bzip2)
bzip2.writer('foo.dat.bz2').copyfrom('foo.dat')

9.2Module Function

The bzip2 module provides following functions:

bzip2.reader(stream:stream:r) {block?}

Creates a stream instance that decompresses bzip2 data from the specified stream that has readable attribute.

If block is specified, it would be evaluated with a block parameter |st:stream|, where st is the created instance. In this case, the block's result would become the function's returned value.

bzip2.writer(stream:stream:w, blockSize100k?:number) {block?}

Creates a stream instance that compresses data into bzip2 format and writes it to the specified stream that has writable attribute.

The argument blockSize100k takes a number between 1 and 9 that specifies the block size to be used for compression. The actual block size is 100000 times of this value. Nine gives the best compression but takes most memory.

If block is specified, it would be evaluated with a block parameter |st:stream|, where st is the created instance. In this case, the block's result would become the function's returned value.

9.3Extension to stream Class

The bzip2 module extends the stream class with following methods:

stream#reader@bzip2() {block?}

Creates a stream instance that decompresses bzip2 data from the specified stream that has readable attribute.

If block is specified, it would be evaluated with a block parameter |st:stream|, where st is the created instance. In this case, the block's result would become the function's returned value.

stream#writer@bzip2(blockSize100k?:number) {block?}

Creates a stream instance that compresses data into bzip2 format and writes it to the specified stream that has writable attribute.

The argument blockSize100k takes a number between 1 and 9 that specifies the block size to be used for compression. The actual block size is 100000 times of this value. Nine gives the best compression but takes most memory.

If block is specified, it would be evaluated with a block parameter |st:stream|, where st is the created instance. In this case, the block's result would become the function's returned value.

9.4Thanks

The bzip2 module uses libbzip2 which is distributed in the following site:

http://www.bzip.org/