Java 例外:ZLIB 输入流意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24531089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception: Unexpected end of ZLIB input stream
提问by Ryoichiro Oka
There is something wrong with GZIPInputStream
or GZIPOutputStream
. Just please read the following code (or run it and see what happens):
有什么问题GZIPInputStream
或GZIPOutputStream
。请阅读以下代码(或运行它,看看会发生什么):
def main(a: Array[String]) {
val name = "test.dat"
new GZIPOutputStream(new FileOutputStream(name)).write(10)
println(new GZIPInputStream(new FileInputStream(name)).read())
}
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.
它创建一个文件test.dat
,10
通过 GZIP写入单个字节格式,并以相同格式读取同一文件中的字节。
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.
由于某种原因,阅读线似乎走错了路。
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!
我在谷歌上搜索了错误Unexpected end of ZLIB input stream
并发现了一些错误报告给 Oracle,这些报告是在 2007-2010 年左右发布的。所以我想这个错误在某种程度上仍然存在,但我不确定我的代码是否正确,所以让我在这里发布并听取您的建议。谢谢!
采纳答案by Stephen C
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.)
你要叫close()
上GZIPOutputStream
你试图读取它之前。文件的最后字节只会在文件实际关闭时写入。(这与输出堆栈中的任何显式缓冲flush()
无关。当您告诉它关闭时,流只知道压缩和写入最后一个字节。A可能无济于事......尽管调用finish()
而不是close()
应该工作。看看文档。)
Here's the correct code (in Java);
这是正确的代码(在 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());
}
}
(I've not implemented resource management properly. Don't treat this as an example of "good code".)
(我没有正确实现资源管理。不要把它当作“好代码”的例子。)