Using gzip, tell() returns the offset in the uncompressed file.
In order to show a progress bar, I want to know the original (uncompressed) size of the file.
Is there an easy way to find out?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
|
The gzip format specifies a field called
In gzip.py, which I assume is what you're using for gzip support, there is a method called
There you can see that the I just looked all of this up right now, and I haven't tried it so I could be wrong. I hope this is of some use to you. Sorry if I misunderstood your question. |
|||||||||||||||||||||
|
|
Uncompressed size is stored in the last 4 bytes of the gzip file. We can read the binary data and convert it to an int. (This will only work for files under 4GB)
|
|||||||||
|
|
Unix way: use "gunzip -l file.gz" via subprocess.call / os.popen, capture and parse its output. |
|||||||||||||
|
|
The last 4 bytes of the .gz hold the original size of the file |
|||||
|
|
|||
|
|
|
I am not sure about performance, but this could be achieved without knowing
This should also work for other (compressed) stream readers like EDIT:
as suggested in the comments, |
|||||
|
|
Looking at the source for the
? Maybe it would be good to do some sanity checking before doing that, like checking that the attribute exists with Not exactly a public API, but... |
|||||||||
|
|
GzipFile.size stores the uncompressed size, but it's only incremented when you read the file, so you should prefer len(fd.read()) instead of the non-public GzipFile.size. |
|||||||||
|
|
|||
|
|