`
cin_ie
  • 浏览: 46981 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

GZIPInputStream的bug

阅读更多
关于GZIPInputStream的bug,在jdk的最新版本上竟然还没解决这个问题。用到gzip的需要注意了:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4691425

问题描述:

在使用GZIPInputStream对gizp文件进行读取的时候,使用read方法,方法返回-1,表示该文件读取真正的结束,但是实际却并不是这样。


这些文件在windowsxp用win-rar解压或者linux上zcat aaa.gz命令进行解压后,是可以获得文件的全部内容的。但是在使用GZIPInputStream却不行。

这个bug是java在读取gzip文件的时候,还没有读取完毕,过早的返回-1,造成有部分文件没有读取完毕。

不过好在已经有人解决这个问题了,下面是解决的方法:



package x.y.z;

import java.io.InputStream;
import java.io.PushbackInputStream;
import java.io.IOException;

public
class MultiMemberGZIPInputStream extends GZIPInputStream {

    public MultiMemberGZIPInputStream(InputStream in, int size) throws IOException
    {
        // Wrap the stream in a PushbackInputStream…
        super(new PushbackInputStream(in, size), size);
        this.size=size;
    }

    public MultiMemberGZIPInputStream(InputStream in) throws IOException
    {
        // Wrap the stream in a PushbackInputStream…
        super(new PushbackInputStream(in, 1024));
        this.size=-1;
    }

    private MultiMemberGZIPInputStream(MultiMemberGZIPInputStream parent) throws IOException
    {
        super(parent.in);
        this.size=-1;
        this.parent=parent.parent==null ? parent : parent.parent;
        this.parent.child=this;
    }

    private MultiMemberGZIPInputStream(MultiMemberGZIPInputStream parent, int size) throws IOException
    {
        super(parent.in, size);
        this.size=size;
        this.parent=parent.parent==null ? parent : parent.parent;
        this.parent.child=this;
    }

    private MultiMemberGZIPInputStream parent;
    private MultiMemberGZIPInputStream child;
    private int size;
    private boolean eos;

    public int read(byte[] inputBuffer, int inputBufferOffset, int inputBufferLen) throws IOException {

        if (eos) { return -1;}
        if (this.child!=null)
            return this.child.read(inputBuffer, inputBufferOffset, inputBufferLen);

        int charsRead=super.read(inputBuffer, inputBufferOffset, inputBufferLen);
        if (charsRead==-1)
        {
            // Push any remaining buffered data back onto the stream
            // If the stream is then not empty, use it to construct
            // a new instance of this class and delegate this and any
            // future calls to it…
            int n = inf.getRemaining() – 8;
            if (n > 0)
            {
                // More than 8 bytes remaining in deflater
                // First 8 are gzip trailer. Add the rest to
                // any un-read data…
                ((PushbackInputStream)this.in).unread(buf, len-n, n);
            }
            else
            {
                // Nothing in the buffer. We need to know whether or not
                // there is unread data available in the underlying stream
                // since the base class will not handle an empty file.
                // Read a byte to see if there is data and if so,
                // push it back onto the stream…
                byte[] b=new byte[1];
                int ret=in.read(b,0,1);
                if (ret==-1)
                {
                    eos=true;
                    return -1;
                }
                else
                    ((PushbackInputStream)this.in).unread(b, 0, 1);
            }

            MultiMemberGZIPInputStream child;
            if (this.size==-1)
                child=new MultiMemberGZIPInputStream(this);
            else
                child=new MultiMemberGZIPInputStream(this, this.size);
            return child.read(inputBuffer, inputBufferOffset, inputBufferLen);
        }
        else
            return charsRead;
    }

}

重写了GZIPInputStream这个类。然后你在使用的时候直接引入MultiMemberGZIPInputStream这个类,并调用即可。

例如:

  BufferedReader     bufferedReader = new BufferedReader(new InputStreamReader(new MultiMemberGZIPInputStream(new FileInputStream(gzFile)),


分享到:
评论
4 楼 流氓24# 2013-05-22  
知道了,强转些就好了
3 楼 流氓24# 2013-05-22  
inf.getRemaining() – 8;  是什么啊,为什么跌进来就报错。
2 楼 小白茶1112 2013-04-22  
多谢大神,帮了大忙
1 楼 Williams_Glee 2012-11-21  

相关推荐

    重写GZIPInputStream中相应方法MultiMemberGZIPInputStream

    当使用jdk中GZIPInputStream读取.gz文件时,有时还未到文件结尾,则也会返回-1,该工具类解决了此bug

    解压gz文件的jar包及java代码

    GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(outputFilePath); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > 0) ...

    归属地数据

    要使用GZIPInputStream读取“location”这样的压缩文件,首先需要创建一个FileInputStream对象来指向原始的GZIP文件,然后用GZIPInputStream包装这个FileInputStream。以下是一个简单的示例代码: ```java import ...

    sharp zipsharp zip

    GZipInputStream gzi = new GZipInputStream(response.ResponseStream); MemoryStream ms = new MemoryStream(); int count=0; byte[] uncompress =new byte[1024]; while ((count = gzi.Read(uncompress, 0, ...

    基于Java的实例源码-用GZIP压缩解压文件.zip

    GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream("uncompressed_file"); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > ...

    iOS gzip压缩数据

    在iOS开发中,数据压缩是一种常见的优化手段,可以有效地减小数据传输的体积,提高网络通信的效率。本文将深入探讨“iOS gzip压缩数据”这一主题,包括gzip压缩的原理、如何在iOS应用中实现gzip压缩,以及使用ASI...

    JAVA GZIP压缩案例附带httpwatch监测工具以及使用说明

    GZIPInputStream gis = new GZIPInputStream(bais); int read; byte[] buffer = new byte[1024]; while ((read = gis.read(buffer)) != -1) { System.out.print(new String(buffer, 0, read, "UTF-8")); } }...

    Java用GZIP压缩解压文件源码

    GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) != -1) { ...

    Java用GZIP压缩解压文件.rar

    首先,让我们了解Java中的GZIPOutputStream和GZIPInputStream类,它们是处理GZIP压缩和解压缩的核心工具。`java.util.zip.GZIPOutputStream`用于将数据写入GZIP格式的流,而`java.util.zip.GZIPInputStream`则用于从...

    GZip_java_referffn_gzip_

    GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream("output.txt"); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > 0) { ...

    归属地数据库

    `GZIPInputStream`是Java标准库`java.util.zip`包中的类,用于处理GZIP格式的压缩文件。GZIP是一种广泛使用的文件压缩格式,常用于在网络上传输数据,因为它可以减少文件的大小,加快传输速度。`GZIPInputStream`...

    java解压linux上的压缩文件gz格式文件

    GZIPInputStream in = new GZIPInputStream(new FileInputStream(inFilename)); // 打开输出文件 String outFilename = "outfile"; OutputStream out = new FileOutputStream(outFilename); // 将压缩文件中...

    java压缩+解压GZ(Linux)文件.rar

    GZIPInputStream gis = new GZIPInputStream(fis); BufferedReader br = new BufferedReader(new InputStreamReader(gis)); String line; while ((line = br.readLine()) != null) { System.out.println(line);...

    读取网页源码(不会出现网页乱码)

    GZIPInputStream gzipInputStream = new GZIPInputStream(fis); InputStreamReader reader = new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8); BufferedReader bufferedReader = new ...

    gzipDemo.rar

    using (GZipInputStream gzipStream = new GZipInputStream(compressedFileStream)) using (FileStream decompressedFileStream = File.Create("解压缩后文件路径")) { gzipStream.CopyTo(decompressedFileStream);...

    基于java的用GZIP压缩解压文件.zip

    GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream decompressedFos = new FileOutputStream("decompressed"); BufferedInputStream bis = new BufferedInputStream(gis)) { byte[] buffer = ...

    j2me gzip压缩总汇

    1. **创建GZIPInputStream**: 当接收到gzip压缩的数据时,创建一个`GZIPInputStream`,同样包裹在输入流之上,如`DataInputStream`或`FileInputStream`。 2. **读取数据**: 使用`read()`方法从`GZIPInputStream`...

    Java用GZIP压缩解压文件.7z

    1. **创建GZIPInputStream**: 使用`GZIPInputStream`可以从GZIP格式的流中读取数据。同样,我们需要一个输入流指向压缩文件,然后创建`GZIPInputStream`。 ```java FileInputStream fis = new FileInputStream(...

    使用GZip解压文件

    GZIPInputStream gin = new GZIPInputStream(in); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(decompressedFile))) { byte[] buffer = new byte[1024]; int len; while ((len...

    Java用GZIP压缩解压文件

    在GZIP操作中,我们主要会用到`GZIPOutputStream`和`GZIPInputStream`这两个类,它们分别用于文件的压缩和解压缩。 **文件压缩** 要使用GZIP压缩文件,我们首先创建一个`FileOutputStream`对象,然后通过它创建一...

Global site tag (gtag.js) - Google Analytics