4

I am using Python 2.7. I have a .bz2 file, and I need to figure out the uncompressed file size of its component file without actually decompressing it. I have found ways to do this for gzip and tar files. Anyone know of a way for bz2 files?

Thanks very much

Nupur
  • 1,051
  • 6
  • 15
  • 20

3 Answers3

7

As the other answers have stated, this is not possible without decompressing the data. However, if the size of the decompressed data is large, this can be done by decompressing it in chunks and adding the size of the chunks:

>>> import bz2
>>> with bz2.BZ2File('data.bz2', 'r') as data:
...     size = 0
...     chunk = data.read(1024)
...     while chunk:
...         size += len(chunk)
...         chunk = data.read(1024)
... 
>>> size
11107

Alternatively (and probably faster, though I haven't profiled this) you can seek() to the end of the file and then use tell() to find out how long it is:

>>> import bz2
>>> import os
>>> with bz2.BZ2File('data.bz2', 'r') as data:
...     data.seek(0, os.SEEK_END)
...     size = data.tell()
...
>>> size
11107L
Blair
  • 15,356
  • 7
  • 46
  • 56
  • I didn't try the chunk method, but the seek() & tell() method describe here worked perfectly. – skrrgwasme Sep 08 '14 at 22:08
  • I did a test on the second way using `seek()` & `tell()` on a file with uncompressed size of about 1.2G. It seems that there is not much different with uncompressing the file and count the size in terms of time spent. – Jason Liu Jan 11 '21 at 09:08
2

I suspect this is impossible due to the nature of bz2 format and compressing techniques it uses. Here is a quite good description of the both format and the algorithms http://en.wikipedia.org/wiki/Bzip2#File_format

You will never know original data size until you decompress it.

Maksym Polshcha
  • 18,030
  • 8
  • 52
  • 77
0

It seems that telling the size of bz2 file without actually decompressing it is impossible. See the link for more details and a possible solution: https://superuser.com/questions/53984/is-there-a-way-to-determine-the-decompressed-size-of-a-bz2-file

Community
  • 1
  • 1
Dany
  • 179
  • 1
  • 2
  • 10