Determine uncompressed size of GZIP file

For some applications it is useful to determine the uncompressed size of a file that has been compressed by the gzip algorithm. From the command line this can be done by using the -l option of the gzip program. But this is less straightforward using the Java API's. The GZIPInputStream class does not provide a method to query the original file size. However the information is present in the GZIP file for files that were originally smaller than 4 Gb and can be extracted.

gzip -l compressedfile.gz

This will give you something like this:

compressed uncompressed ratio uncompressed_name
15024079 50187117 70.1% compressedfile

If you want to determine the uncompressed size of a gzip file from within a program, you can extract to original file size from the gzip file. This size is stored in the last 4 bytes of the file. This will only provide the correct value if the compressed file was smaller than 4 Gb.

To extract this information and convert it to something useful in your Java program.

RandomAccessFile raf = new RandomAccessFile(file, "r"); - 4);
int b4 =;
int b3 =;
int b2 =;
int b1 =;
int val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;

The original size of the compressed file will now be available in the val variable.


Unit of val ?


Thanks for this solution that I will use ;)

I've juste a question :
What is the unit of val ? Is it bytes ?

The unit of val is indeed

The unit of val is indeed bytes