To retrieve the uncompressed size of a file that is compressed via gzip, you can read the last four bytes. I am doing this to see if there are any files that are not the size they are supposed to be. If a file is smaller than it should be, I use this code to append to the file:
GZIPOutputStream gzipoutput = new GZIPOutputStream
(new FileOutputStream(file, true));
while ((len=bs.read(buf)) >= 0) {
gzipoutput.write(buf, 0, len);
}
gzipoutput.finish();
gzipoutput.close();
Of course, this appends to the end of the gzip file as expected. However, after the append, reading the last four bytes of the gzip file (to get the uncompressed file size), does not give me expected results. I suspect that it is because using the GZIPOutputStream does not correctly append the size bytes to the end of the file.
How can I modify my code so that the correct size bytes are appended?
EDIT
I am reading the bytes in little-endian order, like so:
gzipReader.seek(gzipReader.length() - 4);
int byteFour = gzipReader.read();
int byteThree = gzipReader.read();
int byteTwo = gzipReader.read();
int byteOne = gzipReader.read();
// Now combine them in little endian
long size = ((long)byteOne << 24) | ((long)byteTwo << 16) | ((long)byteThree << 8) | ((long)byteFour);
I was thinking that since I was appending to a gzip file, it only wrote the bytes appended instead of the total file size. Is that plausible?