您的位置 首页 java

实现读取Gzip

一、读取Gzip的文件

BufferedReader br = new BufferedReader(new InputStream Reader(new MultiMemberGZIPInputStream(new PushbackInputStream( new BufferedInputStream(new File input Stream(filePath))), 1024)));

 BufferedReader br = new BufferedReader(new InputStreamReader(new MultiMemberGZIPInputStream(new PushbackInputStream( new BufferedInputStream(new FileInputStream(filePath))), 1024)));  

二、网上有些网友说通过继承 GZIPInputStream 可以解决 Gzip 部分问题文件读取的时候会报错 java .io.IO Exception : Not in GZIP format ,经过尝试还是继续报错

 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 {
        super(new PushbackInputStream(in, size), size);
        this.size = size;
    }

    public MultiMemberGZIPInputStream(PushbackInputStream pushbackInputStream) throws IOException {
        super(pushbackInputStream);
        this.size = -1;
    }

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

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

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

    @Override
    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) {
            int n = inf.getRemaining() - 8;
            if (n > 0) {
                ((PushbackInputStream) this.in).unread(buf, len - n, n);
            } else {
                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 tmpChild;
            if (this.size == -1) {
                tmpChild = new MultiMemberGZIPInputStream(this);
            } else {
                tmpChild = new MultiMemberGZIPInputStream(this, this.size);
            }
            return tmpChild.read(inputBuffer, inputBufferOffset, inputBufferLen);
        } else {
            return charsRead;
        }
    }
}
  

文章来源:智云一二三科技

文章标题:实现读取Gzip

文章地址:https://www.zhihuclub.com/181302.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图