GZIPOutputStream not updating Gzip size bytes

蹲街弑〆低调 提交于 2019-12-11 09:59:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25436895/gzipoutputstream-not-updating-gzip-size-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!