Gets the uncompressed size of this GZIPInputStream?

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

    If you can guess at the compression ratio (a reasonable expectation if the data is similar to other data you've already processed), then you can work out the size of arbitrarily large files (with some error). Again, this assumes a file containing a single gzip stream. The following assumes the first size greater than 90% of the estimated size (based on estimated ratio) is the true size:

    estCompRatio = 6.1;
    RandomAccessFile raf = new RandomAccessFile(inputFilePath + ".gz", "r");
    compLength = raf.length();
    byte[] bytes = new byte[4];
    raf.read(bytes);
    uncLength = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
    raf.seek(compLength - 4);
    uncLength = raf.readInt();
    while(uncLength < (compLength * estCompRatio * 0.9)){
      uncLength += (1L << 32);
    }
    

    [setting estCompRatio to 0 is equivalent to @Alexander's answer]

提交回复
热议问题