SUN提供的关于GZ压缩包解压的 BUG

 

先看看下面解压类

public static void main(String[] args) {
        
           
try

            {
               
int
nnumber;
FileInputStream fin
=new FileInputStream("c://abc//dddd.log.done.gz");

GZIPInputStream gzin=new GZIPInputStream(fin);
FileOutputStream fout
=new FileOutputStream("c://abc//cc.log"
);
byte[] buf=new byte[1024
];    
           
               
while ((nnumber=gzin.read(buf,0,buf.length)) != -1
)
                fout.write(buf,
0
,nnumber);
           
           
                gzin.close();
                fout.close();
                fin.close();
                }
               
catch
(IOException e)
                {
                System.out.println(e);
            }

      }
    }

GZIPInputStream 这个类的read()方法有BUG

是什么BUG呢?这个 也是我去SUN的官方论坛看到有些人提到该类,也说是BUG.

read只能读出前面几行的数据

如果数据量大的话 后面的大部分数据就读不出来了.

下面重写了该类(解决了该问题)

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.zip.GZIPInputStream;

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;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值