`
cin_ie
  • 浏览: 45897 次
  • 性别: 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

    OC GZIP解压/压缩ISO-8859-1编码格式

    类似于安卓GZIPInputStream

    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, ...

    autojsdemo.zip

    1688个autojs的js文件源码,从简单到复杂,应有尽有,有兴趣的可以下载了认真学习,能提供不少思路

    GZip流压缩&Web流压缩组件

     第一个软件,传输流/文件的压缩,利用Internet中惯常使用的gzip, deflate算法,可以和sun java的GZipInputStream和GZipOutputStream互操作  第二个软件,基于HTTP的压缩传输软件,可完全和IE的VBScript和...

    归属地数据库

    使用java GZIPInputStream可以读取,做来电显归属软件可以用

    归属地数据

    使用java GZIPInputStream可读取,做来电归属地软件的开发可以用

    C#压缩与解压代码

    功能:解压或压缩zip格式的文件,里面附带...生成一个GZipInputStream流,用来打开压缩文件, 因为GZipInputStream由Stream派生,所以它可以赋给Stream。 它的构造函数的参数是一个表示要解压的压缩文件所代表的文件流

    zipme包---j2me的zip

    GZIPInputStream This filter stream is used to decompress a "GZIP" format stream. GZIPOutputStream This filter stream is used to compress a stream into a "GZIP" stream. Inflater Inflater is used to ...

    Android实现压缩字符串的方法示例

    前言 Android端可以对字符串进行压缩,我们在进行大量简单文本传输时,可以先压缩字符串再发送。接收端接收后再解压。也可以将字符串压缩后...import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutput

Global site tag (gtag.js) - Google Analytics