class bz2.BZ2Decompressor()
>>> import bz2 >>> obj = bz2.BZ2Decompressor() >>> obj <_bz2.BZ2Decompressor object at 0x00000238485F2DF0>
decompress
(data, max_length=-1)Takes bytes data and returns decompressed data also as bytes. max_length specifies the byte size to be decompressed.
eof
True
if the end-of-stream marker has been reached.
unused_data
Data found after the end of the compressed stream.
If this attribute is accessed before the end of the stream has been reached, its value will be b''
needs_input
False
if the decompress()
method can provide more decompressed data before requiring new uncompressed input.
needs_input
set to True.needs_input
set to False. To get the data left in the internal buffer, pass b'' in the next call to decompress()
.unused_data
.import sys import bz2 class DecompressData(): self.prev_data = b'' self.decomp = None def __init__(self, size): self.decomp = bz2.BZ2Decompressor() self.prev_data = b'' self.chunk_size = size def decompress(self, iterable, output): for chunk in iterable: dc = self.decomp.decompress(chunk) output.write(prev_data + dc) self.prev_data = self.decomp.unused_data # if the eof has reached, we need a new object if self.decomp.eof == True: reset() def reset(self): self.decomp = bz2.BZ2Decompressor() def main(): # suppose sample.bz2 is a large file and # cannot be decompressed in one go file_path = 'sample.bz2' # size in bytes to decompress size = 1024 obj = DecompressData(size) # here, we are printing the decompressed object to terminal console with open(file_path, 'r') as f: iterable = iter(lambda: f.read(size), b'') obj.decompress(iterable, sys.stdout.buffer) f.close() if __name__ == '__main__': main()
Name | Views | Likes |
---|---|---|
Python NLP Introduction | 316 | 0 |
Python Compressing large size files using bz2 | 684 | 0 |
Python bz2 Introduction | 339 | 0 |
Python Decompressing large size files using bz2 library | 1297 | 0 |
Comments