GZIPOutputStream does not update Gzip size bytes

To extract the uncompressed size of a gzip compressed file, you can read the last four bytes. I do this to see if there are files that are not the size they should be. If the 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 adds to the end of the gzip file as expected. However, once added, reading the last four bytes of the gzip file (to get the size of the uncompressed file) does not give the expected results. I suspect this is because using GZIPOutputStream incorrectly adds size bytes to the end of the file.

How can I modify my code to add the correct size bytes?

EDIT

I am reading bytes in little-endian order, for example:

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 thought that since I was adding the gzip file it was only writing bytes, not the total file size. Is this believable?

+1


source to share


1 answer


since I was adding the gzip file, it only wrote bytes, not the total file size. Is this believable?



Not only plausible, but inevitable. Take a look at your code. How exactly GZIPOutputStream

will the append know the previous file size? All he can see is incoming data and outgoingOutputStream.

+1


source







All Articles