Gets the uncompressed size of this GZIPInputStream?

后端 未结 8 773
余生分开走
余生分开走 2021-01-04 13:51

I have a GZIPInputStream that I constructed from another ByteArrayInputStream. I want to know the original (uncompressed) length for the gzip data.

8条回答
  •  佛祖请我去吃肉
    2021-01-04 14:31

    It is possible to determine the uncompressed size by reading the last four bytes of the gzipped file.

    I found this solution here:

    http://www.abeel.be/content/determine-uncompressed-size-gzip-file

    Also from this link there is some example code (corrected to use long instead of int, to cope with sizes between 2GB and 4GB which would make an int wrap around):

    RandomAccessFile raf = new RandomAccessFile(file, "r");
    raf.seek(raf.length() - 4);
    byte b4 = raf.read();
    byte b3 = raf.read();
    byte b2 = raf.read();
    byte b1 = raf.read();
    long val = ((long)b1 << 24) | ((long)b2 << 16) | ((long)b3 << 8) | (long)b4;
    raf.close();
    

    val is the length in bytes. Beware: you can not determine the correct uncompressed size, when the uncompressed file was greater than 4GB!

提交回复
热议问题