uncompressed size of .gz file

Fredrik Lundh fredrik at pythonware.com
Sun Sep 19 04:35:10 EDT 2004


"frankabel at tesla.cujae.edu.cu" wrote:

> What python function give me the uncompressed size of .gz file like
> "gzip -l name_of_compress_file".

the size is stored as a 32-bit integer at the end of the file.  to get it, you
can use something like:

def getsize(gzipfile):
    import struct
    f = open(gzipfile, "rb")
    if f.read(2) != "\x1f\x8b":
        raise IOError("not a gzip file")
    f.seek(-4, 2)
    return struct.unpack("<i", f.read())[0]

usage:

>>> print getsize("Python-2.4a3.tgz")
38758400

hope this helps!

</F> 






More information about the Python-list mailing list