GZIPOutputStream不更新Gzip大小字节 [英] GZIPOutputStream not updating Gzip size bytes

查看:119
本文介绍了GZIPOutputStream不更新Gzip大小字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检索通过gzip压缩的文件的未压缩大小,可以读取最后四个字节。我这样做是为了查看是否有文件大小不应该达到的大小。如果文件小于应有的大小,我将使用以下代码将其追加到文件中:

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();

当然,这会按预期附加到gzip文件的末尾。但是,在追加之后,读取gzip文件的最后四个字节(以获取未压缩的文件大小)不会给我预期的结果。我怀疑这是因为使用GZIPOutputStream不能正确地将大小字节附加到文件末尾。

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

我正在读取little-endian中的字节顺序,如下所示:

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);

我在想,因为我要追加到gzip文件,所以它只写了追加的字节而不是文件总大小。

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?

推荐答案


因为我要附加到gzip文件,所以它只写字节而不是总文件大小。

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

不仅合理,而且不可避免。看一下你的代码。附加 GZIPOutputStream 到底如何知道文件的先前大小?它只能看到传入的数据和传出的 OutputStream。

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.

这篇关于GZIPOutputStream不更新Gzip大小字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆