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. mysql查询今天、昨天、本周、本月、上一月 、今年数据

    mysql数据库中的关于查询日期的一些操作如下: --今天 select * from 表名 where to_days(时间字段名) = to_days(now()); --昨天 --本周 SELE ...

  2. MySQL通过Explain查看select语句的执行计划结果触发写操作

    [背景] 某某同学执行了一下Explain结果结果发现数据库有了一条写入操作,恭喜这位同学你的锅到货了,你签收一下: 对! 你没有听错,在一种场景下就算是Explain也会引发数据的写操作,就这是外层 ...

  3. pandas数组(pandas Series)-(2)

    pandas Series 比 numpy array 要强大很多,体现在很多方面 首先, pandas Series 有一些方法,比如: describe 方法可以给出 Series 的一些分析数据 ...

  4. [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.

    Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...

  5. 如何让 zend studio 10 识别 Phalcon语法并且进行语法提示

    让 zend studio 10 识别 Phalcon语法并且进行语法提示 https://github.com/rogerthomas84/PhalconPHPDoc 下载解压后,把里面 phalc ...

  6. android Socket 编程

    Socket 通信 1.UDP实现  (DatagramSocket) [客户端] //首先创建一个DatagramSocket对象 DatagramSocket socket = new Datag ...

  7. kindeditor自定义插件插入视频代码

    kindeditor自定义插件插入视频代码 1.添加插件js 目录:/kindeditor/plugins/diy_video/diy_video.js KindEditor.plugin('diy_ ...

  8. kali下添加PATH环境变量

    添加PATH环境变量,第1种方法: [root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@l ...

  9. 【转】asp.net中@page指令的属性Inherits、Src、CodeBehind区别

    Inherits.Src.CodeBehind 在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. 通常一个 @page 指令 ...

  10. LeetCode: Palindrome Partitioning 解题报告

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...