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");
raf.seek(raf.length() - 4);
int b4 = raf.read();
int b3 = raf.read();
int b2 = raf.read();
int b1 = raf.read();
int val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
raf.close();

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

Comments

Unit of val ?

Hello,

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