• java中GZIPOutputStream 流的使用(EOFException)


    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
  • 相关阅读:
    PHP 中 define() 和 const 定义常量时的区别
    tar 解压常用压缩文件格式命令大全
    JVM的GC策略
    守护线程(Daemon Thread)
    探索HashMap实现原理及其在jdk8数据结构的改进
    Java面试& HashMap实现原理分析
    Java分布式应用
    全面理解Java内存模型
    Java& Vector与ArrayList区别
    深入理解Java虚拟机&运行时数据区
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7374487.html
Copyright © 2020-2023  润新知