龙空技术网

实现读取Gzip

从事java的小白 103

前言:

如今同学们对“java读取zip”大体比较关注,朋友们都想要知道一些“java读取zip”的相关内容。那么小编同时在网摘上网罗了一些有关“java读取zip””的相关文章,希望小伙伴们能喜欢,小伙伴们快快来学习一下吧!

一、读取Gzip的文件

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

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

二、网上有些网友说通过继承GZIPInputStream可以解决Gzip部分问题文件读取的时候会报错java.io.IOException: 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;        }    }}

标签: #java读取zip #apachegzip报错