在解压gz文件时,如果直接用java.util.zip.GZIPInputStream来处理问题只能解压很少一部分内容,通过类MultiMemberGZIPInputStream 可以完全解压一个gz文件。
2011-03-26 00:38
1191 查看
在解压gz文件时,如果直接用java.util.zip.GZIPInputStream来处理问题只能解压很少一部分内容,通过类MultiMemberGZIPInputStream 可以完全解压一个gz文件。
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;
}
}
应用示例:
try {
int nnumber;
FileInputStream fin = new FileInputStream(gzPath);
MultiMemberGZIPInputStream MmGz = new MultiMemberGZIPInputStream(fin);
FileOutputStream fout = new FileOutputStream(topath);
byte[] buf = new byte[1024];
nnumber = MmGz.read(buf, 0, buf.length);
while (nnumber != -1) {
fout.write(buf, 0, nnumber);
nnumber = MmGz.read(buf, 0, buf.length);
}
MmGz.close();
fout.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
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;
}
}
应用示例:
try {
int nnumber;
FileInputStream fin = new FileInputStream(gzPath);
MultiMemberGZIPInputStream MmGz = new MultiMemberGZIPInputStream(fin);
FileOutputStream fout = new FileOutputStream(topath);
byte[] buf = new byte[1024];
nnumber = MmGz.read(buf, 0, buf.length);
while (nnumber != -1) {
fout.write(buf, 0, nnumber);
nnumber = MmGz.read(buf, 0, buf.length);
}
MmGz.close();
fout.close();
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
相关文章推荐
- Java Gzip 不解压,直接读取txt文件内容
- 如何导出Java项目成为一个可以直接双击运行的jar文件的问题
- java.util.zip.GZIPInputStream.readUByte,Not in GZIP format错误处理
- java.util.zip.GZIPInputStream.readUByte,Not in GZIP format错误处理
- Java无需解压直接读取Zip文件和文件内容
- 一个用java.util.zip创建和读取zip文件的类,可以操作目录
- 使用java.util.zip压缩、解压文件
- java不解压读取linux服务器上的zip以及tar.gz压缩文件
- Java采用readInt()方法直接读取特定的ZIP压缩文件出现了奇怪的问题
- JAVA工具类(11)---读取资源文件工具类ResourcesUtil (可以动态更改值的内容)
- java.util.zip包无法正常解压rar软件压缩的rar文件
- Java无需解压直接读取Zip文件和文件内容
- Java无需解压直接读取Zip文件和文件内容
- 习惯把运行的class的名字作为文件名保存 如果文件中类是public修饰,那么类名必须和文件名相同 一个java文件中可以放几个public的类??? java变量:变量是用来标识一块内存的,变量必
- java.util.zip对zip文件解压
- 002_009 Python 从Zip中读取数据 直接检查一个zip格式的归档文件部分或所有文件而且不用解压
- 在任何一个java工程下(即便是j2ee工程)都可以直接对某一个java文件run as Java Application,但是为什么有的java文件右击,没有“Java Application”
- 002_010 Python 处理字符串中的Zip文件 你的程序收到了一个字符串,内容是一个ZIP文件,需要读取zip文件的信息
- 在页面中,我们经常看到,一个button按钮,如果属标点击,就会触发一个窗口的显示,如果二次点击并可以隐藏,那么如何通过JAVA配合html来实现这一功能呢?
- java 压缩以及解压文件,tar,zip,gz(gizp)