Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

compression - Python bz2 uncompressed file size

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...