GZip流的使用非常多人都出现了以下的异常:java.io.EOFException: Unexpected end of ZLIB input stream。或者出现压缩后的数据不全的情况(就是压缩的数据仅仅是原数据的一部分,不能被解压缩)

原因是在使用GZIPOutputStream 对象的时候没有调用close方法.

如:

@org.junit.Test
public void test(){
try {
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] original="asdfasfasfasfsafsadfasfasfasfasfasdfasdasfsasdfasfsafsafsadsafsadsadfasffasdfsafdasf!".getBytes();
System.out.println("original:"+original.length);
//byteArrayOutputStream.write(original);
GZIPOutputStream gzipOutputStream=new GZIPOutputStream(byteArrayOutputStream);
gzipOutputStream.write(original);
//这里一定要先把gzipOutputStream流关闭了,否则得到的是部分数据,而且以下在解压缩的时候会出现EOFException异常
gzipOutputStream.close();
byteArrayOutputStream.close();
byte[] compressByteArray = byteArrayOutputStream.toByteArray();
System.out.println("compress:"+compressByteArray.length);
//解压缩
GZIPInputStream gunzip = new GZIPInputStream(new ByteArrayInputStream(compressByteArray));
ByteArrayOutputStream compressByteArrayOut=new ByteArrayOutputStream();
byte[] buffer=new byte[4096];
int temp=-1;
while((temp=gunzip.read(buffer))>0){
compressByteArrayOut.write(buffer, 0, temp);
}
byte[] unCompressByte = compressByteArrayOut.toByteArray();
System.out.println("uncompress:"+unCompressByte.length);
gunzip.close();
compressByteArrayOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}

上面的代码假设没有调用:gzipOutputStream.close(); 程序将会出现:java.io.EOFException: Unexpected end of ZLIB input stream。

java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:116)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at com.searow.test.Object2ByteArray.test(Object2ByteArray.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

正确的执行结果:

original:87
compress:57
uncompress:87

java中GZIPOutputStream 流的使用(EOFException)的更多相关文章

  1. 理解Java中字符流与字节流的区别

    1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序 ...

  2. Java中IO流的总结

    有关Java中IO流总结图 流分类 按方向分 输入流 输出流 按单位分 字节流 字符流 按功能分 节点流 处理流(过滤流) 其他 所有的流继承与这四类流:InputSteam.OutputStream ...

  3. java中有关流操作的类和接口

    一.java操作l流有关的类和接口 1.File 文件类 2.RandomAccessFile 随机存储文件类 3.InputStream 字节输入流 4.OutputStream 字节输出流 5.R ...

  4. 理解Java中字符流与字节流

    1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个"流动的方向",通常可 ...

  5. 理解Java中字符流与字节流的区别(转)

    1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序 ...

  6. Java中对象流使用的一个注意事项

    再写jsp的实验作业的时候,需要用到java中对象流,但是碰到了之前没有遇到过的情况,改bug改到崩溃!!记录下来供大家分享 如果要用对象流去读取一个文件,一定要先判断这个文件的内容是否为空,如果为空 ...

  7. java 中 IO 流分为几种?(未完成)

    java 中 IO 流分为几种?(未完成)

  8. Java中IO流,输入输出流概述与总结

    总结的很粗糙,以后时间富裕了好好修改一下. 1:Java语言定义了许多类专门负责各种方式的输入或者输出,这些类都被放在java.io包中.其中, 所有输入流类都是抽象类InputStream(字节输入 ...

  9. java中IO流小解

    下面这张图列出了java中一些处理流: java中根据操作对象的不同可以分为:字节流和字符流. 首先我们先表示一下什么叫节点流和处理流: 节点流:可以从或向一个特定的地方(节点)读写数据.如FileR ...

随机推荐

  1. JPA的多表复杂查询

    转 JPA的多表复杂查询:详细篇 原文链接: https://mp.weixin.qq.com/s/7J6ANppuiZJccIVN-h0T3Q 2017-11-10 从小爱喝AD钙  最近工作中由于 ...

  2. thinkphp的系统变量

    define('EXT', '.php'); define('DS', DIRECTORY_SEPARATOR); defined('THINK_PATH') or define('THINK_PAT ...

  3. RSA算法 JS加密 JAVA解密

    有这样一个需求,前端登录的usernamepassword,password必需加密.但不可使用MD5,由于后台要检測password的复杂度,那么在保证安全的前提下将password传到后台呢,答案 ...

  4. SVN不显示对号的解决方法

    具体操作方法如下: 1.到C:\Windows文件夹下,打开regedit.exe 2.Ctrl+F,搜索“ShellIconOverlayIdentifiers” 3.将TortoiseAdded. ...

  5. keepAlive参数详解

    最近研究netty5.0中 发现http例子里面有关于KeepAlive的处理,于是研究了下 http://www.nowamagic.net/academy/detail/23350305

  6. nginx 并发数问题思考:worker_connections,worker_processes与 max clients

    我相信,很多人都跟我一样,看书都不会太细致也不太认真思考,感觉书中讲的东西都应该是对的,最近读书时我发现以前认为理所当然的东西事实上压根都没有弄明白,最终的结果是,书是别人的,书中的知识也是别人的. ...

  7. 每日英语:Surviving a Conference Call

    The conference call is one of the most familiar rituals of office life -- and one of the most hated. ...

  8. 【教程】ubuntu中配置tftp

    1. 安装 tftpd ( tftp 服务器). tftp ( tftp 客户端)以及 xinetd (超级服务器) 1. 安装 tftpd ( tftp 服务器). tftp ( tftp 客户端) ...

  9. sql语句插入百万测试数据

    开发的过程中,很多时候我们需要插入百万数据来测试功能和性能,今天我来教大家最简单的插入方法 -----------------1.新建表--------------------- CREATE TAB ...

  10. LOCAL_EXPORT_C_INCLUDES和LOCALC_INCLUDES 的差别

    http://stackoverflow.com/questions/6595208/what-does-this-line-mean-local-export-c-includes LOCAL_EX ...