All about Python file compressing moduel














































All about Python file compressing moduel



In this article we are going to learn how to compress files in Python using BZ2 module.


Bz2 modules helps to compress and decompress data using BZ2 compressing algorithm.


 

 

Method to open a file:- 

   1.bz2.open(filename, mode='r', compresslevel=9) 

           Parameters taken:- 

  1. Filename :- The path or the name of file that needed to be compressed/decompressed. 

  1. Mode - The mode argument can be any of 'r', 'rb', 'w', 'wb', 'x', 'xb', 'a' or 'ab' for binary mode, or 'rt', 'wt', 'xt', or 'at' for text mode. The default is 'rb'. 

  1. Compresslevel :- It is integer ranging from 1-9, 1 being fastest and lowest compressed and 9 being slowest and most compressed. 

 

Classes and their methods:-

1. class bz2.BZ2Compressor(compresslevel) - Creates a new compressor object. 

Methods used :- 

  1. compress(data) -  Provides data to compressor object, returns byte type data. 

  1. Flush() - Finishes the compression process and returns compressed data left in internal buffers.


2. class bz2.BZ2Decompressor - Creates a new decompressor object. 

Methods used :- 

  1. decompress(data, max_length=-1) - Decompresses byte like object and returns decompressed data as byte. 

  1. Eof :- True if the whole stream is covered, False otherwise. 

  1. unused_data :- It is the data found after reaching end of stream. 

  1. needs_input - False if the decompress() method can provide more decompressed data before requiring new uncompressed input. Else True.


For one shot compression and decompression 

  1. 1. bz2.compress(data, compresslevel=9) - It helps to compress byte like object without using constructors. 

           Parameters taken : 

  1.  Data :- It is byte like object. 

  1.  Compresslevel :- It is an integer ranging from 1 to 9, 1 being lowest compressed and 9 being most compressed. 

                     

 

  1. 2. bz2.decompress(data) -  It helps to decompress byte like object without using constructors.  

              


Both zlib and bz2 does identical operation but in terms of opitimization and efficiency, bz2 is better thus it is preferred over zlib. 

 

Suggestive projects:-  

  1. 1. Schedule backup using this sched and BZ2 module.

  1. 2. Create your own compressing tool using tkinter and BZ2 module.


Comments