例外:ZLIB输入流意外结束 [英] Exception: Unexpected end of ZLIB input stream

查看:903
本文介绍了例外:ZLIB输入流意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GZIPInputStream GZIPOutputStream 有问题。请阅读以下代码(或运行它,看看会发生什么):

There is something wrong with GZIPInputStream or GZIPOutputStream. Just please read the following code (or run it and see what happens):

def main(a: Array[String]) {
    val name = "test.dat"
    new GZIPOutputStream(new FileOutputStream(name)).write(10)
    println(new GZIPInputStream(new FileInputStream(name)).read())
}

它创建一个文件 test.dat ,通过GZIP写一个字节 10 格式化,并以相同的格式读取同一文件中的字节。

It creates a file test.dat, writes a single byte 10 formatting by GZIP, and read the byte in the same file with the same format.

这就是我运行它的原因:

And this is what I got running it:

Exception in thread "main" java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at java.util.zip.GZIPInputStream.read(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at nbt.Test$.main(Test.scala:13)
    at nbt.Test.main(Test.scala)

由于某种原因,阅读线似乎走错了路。

The reading line seems going the wrong way for some reason.

我用Google搜索了错误 ZLIB输入流的意外结束,并发现了一些针对Oracle的错误报告,这些报告是在2007-2010左右发布的。所以我猜这个bug仍然存在,但我不确定我的代码是否正确,所以让我在这里发帖并听取你的意见。谢谢!

I googled the error Unexpected end of ZLIB input stream and found some bug reports to Oracle, which were issued around 2007-2010. So I guess the bug still remains in some way, but I'm not sure if my code is right, so let me post this here and listen to your advice. Thank you!

推荐答案

你必须打电话给 close()在您尝试阅读之前, GZIPOutputStream 。只有在文件实际关闭时才会写入文件的最后字节。 (这与输出堆栈中的任何显式缓冲无关。当您告诉它关闭时,流只知道压缩并写入最后的字节。 flush()可能赢了不帮助......虽然调用 finish()而不是 close()应该可以工作。查看javadocs。 )

You have to call close() on the GZIPOutputStream before you attempt to read it. The final bytes of the file will only be written when the file is actually closed. (This is irrespective of any explicit buffering in the output stack. The stream only knows to compress and write the last bytes when you tell it to close. A flush() probably won't help ... though calling finish() instead of close() should work. Look at the javadocs.)

这是正确的代码(用Java表示);

Here's the correct code (in Java);

package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZipTest {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        String name = "/tmp/test";
        GZIPOutputStream gz = new GZIPOutputStream(new FileOutputStream(name));
        gz.write(10);
        gz.close();
        System.out.println(new GZIPInputStream(new FileInputStream(name)).read());
    }
}

(我没有正确实施资源管理.Don不要把它当作好代码的例子。)

(I've not implemented resource management properly. Don't treat this as an example of "good code".)

这篇关于例外:ZLIB输入流意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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