I have a GZIPInputStream
that I constructed from another ByteArrayInputStream
. I want to know the original (uncompressed) length for the gzip data. Although I can read to the end of the GZIPInputStream
, then count the number, it will cost much time and waste CPU. I would like to know the size before read it.
Is there a similiar method like ZipEntry.getSize()
for GZIPInputStream
:
public long getSize ()
Since: API Level 1
Gets the uncompressed size of this ZipEntry.
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:
[setting estCompRatio to 0 is equivalent to @Alexander's answer]
Get the FileChannel from the underlying FileInputStream instead. It tells you both file size and current position of the compressed file. Example:
There is no reliable way to get the length other than decompressing the whole thing. See Uncompressed file size using zlib's gzip file access function .
No. It's not in the Javadoc => it doesn't exist.
What do you need the length for?
No, unfortunately if you wanted to get the uncompressed size, you would have to read the entire stream and increment a counter like you mention in your question. Why do you need to know the size? Could an estimation of the size work for your purposes?
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 ofint
, to cope with sizes between 2GB and 4GB which would make anint
wrap around):val
is the length in bytes. Beware: you can not determine the correct uncompressed size, when the uncompressed file was greater than 4GB!