Python bz2 module

Python bz2 Module

Bzip2 is an open-source algorithm for file compression and decompression. Python’s bz2 module provides functionality for programmatically implementing the bzip2 algorithm.

The open() function is the module’s primary interface.

open() Function

This function opens a bzip2-compressed file and returns a file object. Files can be opened in binary or text mode with read/write permissions. The function performs compression based on the compressionlevel argument, which is between 1 and 9.

write() Function

This function can be used on file objects when the file is opened in ‘w’ or ‘wb’ mode. In binary mode, it writes compressed binary data to the file. In normal text mode, the file object is wrapped in a TextIOWrapper object to perform encoding.

read() Function

When opened in read mode, this function reads and returns uncompressed data.

The following code writes compressed data to a bzip2 file.

import bz2
f=bz2.open("test.bz2", "wb")
data=b'Welcome to TutorialsPoint'
f.write(data)
f.close()

This will create the file test.bz2 in the current directory. Any decompression tool will show the file ‘test’. To read uncompressed data from the test.bz2 file, use the following code –

import bz2
f=bz2.open("test.bz2", "rb")
data=f.read()
print (data)
b'Welcome to TutorialsPoint'

The bz2 module also defines the BZ2File class. Its objects act as compressors and decompressors, depending on the mode argument to the constructor.

BZ2File() Method

This is the constructor. Like the open() function, it requires file and mode arguments. The compression level defaults to 9 and can be between 1 and 9.

BZ2Compressor() Method

This function returns an object of the incremental compressor class. In this class, each call to the compress() method returns a block of compressed data. Multiple blocks can be concatenated and ultimately written to a bzip2-compressed file.

flush() Method

This method flushes the buffer and returns the data block within it, to be appended to the compressed object.

BZ2Decompressor() Method

This function returns an incremental decompressor object. The decompressed data blocks are concatenated with the flushed data to form the uncompressed data.

The following example first compresses each item in a list object and writes the concatenated bytes object to a file. It then retrieves the data using the BZ2Decompressor object.

import bz2
data=[b'Hello World', b'How are you?', b'welcome to Python']
obj=bz2.BZ2Compressor()
f=bz2.open("test.bz2", "wb")
d1=obj.compress(data[0])
d2=obj.compress(data[1])
d3=obj.compress(data[2])
d4=obj.flush()

compressedobj=d1+d2+d3+d4
f.write(compressedobj)
f.close()

Use the BZ2Decompressor class for decompression.

obj=bz2.BZ2Decompressor()
f=bz2.open("test.bz2", "rb")
data=f.read()
obj.decompress(data)

Leave a Reply

Your email address will not be published. Required fields are marked *