Gets the uncompressed size of this GZIPInputStream?

后端 未结 8 611
余生分开走
余生分开走 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

    A more compact version of the calculation based on the 4 tail bytes (avoids using a byte buffer, calls Integer.reverseBytes to reverse the byte order of read bytes).

    private static long getUncompressedSize(Path inputPath) throws IOException
    {
        long size = -1;
        try (RandomAccessFile fp = new RandomAccessFile(inputPath.toFile(), "r")) {        
            fp.seek(fp.length() - Integer.BYTES);
            int n = fp.readInt();
            size = Integer.toUnsignedLong(Integer.reverseBytes(n));
        }
        return size;
    }
    

提交回复
热议问题