Get uncompressed size of a .gz file in python

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)

import struct

def getuncompressedsize(filename):
    with open(filename, 'rb') as f:, 2)
        return struct.unpack('I',[0]

Leave a Comment