简体   繁体   中英

GZIPOutputStream not updating Gzip size bytes

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?

since I was appending to a gzip file, it only wrote the bytes appended instead of the total file size. Is that plausible?

Not only plausible but inevitable. Have a look at your code. How exactly is the appending GZIPOutputStream going to know the previous size of the file? All it can see is the incoming data and the outgoing OutputStream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM