Skip to content

Instantly share code, notes, and snippets.

@ozanturksever
Last active August 25, 2020 14:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozanturksever/4968827 to your computer and use it in GitHub Desktop.
Save ozanturksever/4968827 to your computer and use it in GitHub Desktop.
python get uncompressed size of a .gz file
def get_uncompressed_size(self, file):
fileobj = open(file, 'r')
fileobj.seek(-8, 2)
crc32 = gzip.read32(fileobj)
isize = gzip.read32(fileobj) # may exceed 2GB
fileobj.close()
return isize
@ozanturksever
Copy link
Author

taken from gzip.py

@fabianschwarzfritz
Copy link

Hi @ozanturksever,
I think I am searching for exactly this functionality. I do have to know how large a file will be after decompressing and unpacking it. How come this coding does work and show me the same result as ?

> gzip -lv mongodb.tar
method  crc     date  time    compressed uncompressed  ratio uncompressed_name
defla cfdff20d Mar 10 15:25      3037694   3102720000  99.9% mongodb.tar

How does this work?

@webysther
Copy link

webysther commented Oct 1, 2018

@fabianschwarzfritz

def get_uncompressed_size(file):
    pipe_in = os.popen('gzip -l %s' % file)
    list_1 = pipe_in.readlines()
    list_2 = list_1[1].split()
    c , u , r , n = list_2
    return int(u)

Source: https://bytes.com/topic/python/answers/36089-uncompressed-size-gz-file

@nerohmot
Copy link

import gzip

def get_uncompressed_size(FileName):
    with gzip.open(FileName, 'rb') as fd:
        fd.seek(0, 2)
        size = fd.tell()
    return size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment