关于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)),
分享到:
相关推荐
当使用jdk中GZIPInputStream读取.gz文件时,有时还未到文件结尾,则也会返回-1,该工具类解决了此bug
GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(outputFilePath); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > 0) ...
要使用GZIPInputStream读取“location”这样的压缩文件,首先需要创建一个FileInputStream对象来指向原始的GZIP文件,然后用GZIPInputStream包装这个FileInputStream。以下是一个简单的示例代码: ```java import ...
GZipInputStream gzi = new GZipInputStream(response.ResponseStream); MemoryStream ms = new MemoryStream(); int count=0; byte[] uncompress =new byte[1024]; while ((count = gzi.Read(uncompress, 0, ...
GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream("uncompressed_file"); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > ...
在iOS开发中,数据压缩是一种常见的优化手段,可以有效地减小数据传输的体积,提高网络通信的效率。本文将深入探讨“iOS gzip压缩数据”这一主题,包括gzip压缩的原理、如何在iOS应用中实现gzip压缩,以及使用ASI...
GZIPInputStream gis = new GZIPInputStream(bais); int read; byte[] buffer = new byte[1024]; while ((read = gis.read(buffer)) != -1) { System.out.print(new String(buffer, 0, read, "UTF-8")); } }...
GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) != -1) { ...
首先,让我们了解Java中的GZIPOutputStream和GZIPInputStream类,它们是处理GZIP压缩和解压缩的核心工具。`java.util.zip.GZIPOutputStream`用于将数据写入GZIP格式的流,而`java.util.zip.GZIPInputStream`则用于从...
GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream("output.txt"); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > 0) { ...
`GZIPInputStream`是Java标准库`java.util.zip`包中的类,用于处理GZIP格式的压缩文件。GZIP是一种广泛使用的文件压缩格式,常用于在网络上传输数据,因为它可以减少文件的大小,加快传输速度。`GZIPInputStream`...
GZIPInputStream in = new GZIPInputStream(new FileInputStream(inFilename)); // 打开输出文件 String outFilename = "outfile"; OutputStream out = new FileOutputStream(outFilename); // 将压缩文件中...
GZIPInputStream gis = new GZIPInputStream(fis); BufferedReader br = new BufferedReader(new InputStreamReader(gis)); String line; while ((line = br.readLine()) != null) { System.out.println(line);...
GZIPInputStream gzipInputStream = new GZIPInputStream(fis); InputStreamReader reader = new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8); BufferedReader bufferedReader = new ...
using (GZipInputStream gzipStream = new GZipInputStream(compressedFileStream)) using (FileStream decompressedFileStream = File.Create("解压缩后文件路径")) { gzipStream.CopyTo(decompressedFileStream);...
GZIPInputStream gis = new GZIPInputStream(fis); FileOutputStream decompressedFos = new FileOutputStream("decompressed"); BufferedInputStream bis = new BufferedInputStream(gis)) { byte[] buffer = ...
1. **创建GZIPInputStream**: 当接收到gzip压缩的数据时,创建一个`GZIPInputStream`,同样包裹在输入流之上,如`DataInputStream`或`FileInputStream`。 2. **读取数据**: 使用`read()`方法从`GZIPInputStream`...
1. **创建GZIPInputStream**: 使用`GZIPInputStream`可以从GZIP格式的流中读取数据。同样,我们需要一个输入流指向压缩文件,然后创建`GZIPInputStream`。 ```java FileInputStream fis = new FileInputStream(...
GZIPInputStream gin = new GZIPInputStream(in); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(decompressedFile))) { byte[] buffer = new byte[1024]; int len; while ((len...
在GZIP操作中,我们主要会用到`GZIPOutputStream`和`GZIPInputStream`这两个类,它们分别用于文件的压缩和解压缩。 **文件压缩** 要使用GZIP压缩文件,我们首先创建一个`FileOutputStream`对象,然后通过它创建一...