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

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

问题描述

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

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 上搜索了错误 Unexpected end of ZLIB input stream 并发现了一些给 Oracle 的错误报告,这些报告是在 2007-2010 年左右发布的.所以我想这个错误在某种程度上仍然存在,但我不确定我的代码是否正确,所以让我在这里发布并听取您的建议.谢谢!

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!

推荐答案

在尝试阅读 GZIPOutputStream 之前,您必须调用 close().只有当流对象实际关闭时才会写入文件的最后字节.

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 stream object is actually closed.

(这与输出堆栈中的任何显式缓冲无关.流只知道在您告诉它关闭时压缩和写入最后一个字节.flush() 无济于事... 虽然调用 finish() 而不是 close() 应该可以工作.看看 javadocs.)

(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() 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();       // Remove this to reproduce the reported bug
        System.out.println(new GZIPInputStream(new FileInputStream(name)).read());
    }
}

(我没有正确实施资源管理或异常处理/报告,因为它们与此代码的目的无关.不要将其视为好代码"的示例;.)

(I've not implemented resource management or exception handling / reporting properly as they are not relevant to the purpose of this code. Don't treat this as an example of "good code".)

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

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