http://cin-ie.iteye.com/blog/859822

关于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;publicclass 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);elsechild=new MultiMemberGZIPInputStream(this, this.size);return child.read(inputBuffer, inputBufferOffset, inputBufferLen);}elsereturn charsRead;}}

重写了GZIPInputStream这个类。然后你在使用的时候直接引入MultiMemberGZIPInputStream这个类,并调用即可。

例如:

BufferedReader     bufferedReader = new BufferedReader(new InputStreamReader(new MultiMemberGZIPInputStream(new FileInputStream(gzFile)),

http://blog.csdn.net/hwq1987/article/details/6279130

在解压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);elsechild = new MultiMemberGZIPInputStream(this, this.size);return child.read(inputBuffer, inputBufferOffset, inputBufferLen);} elsereturn 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();}

关于GZIPInputStream的bug相关推荐

  1. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  2. 微信无法连接服务器501,微信成语猜猜看第501关BUG出现全是英文怎么过解决方法...

    近日因为跳一跳许多微信小程序游戏异常火爆,其中就包括了成语猜猜看游戏,但是很多小伙伴向小编反应游戏之中出现了BUG,那么微信成语猜猜看BUG怎么办呢?为了帮助各位小伙伴,小编特意带来了成语猜猜看BUG ...

  3. vscode 格式化某一段代码_VSCode格式化代码功能失效的bug解决方法

    VSCode格式化代码功能失效的bug解决方法 前不久我装上了 黑苹果,那么为了快速转移开发环境,我使用了VSCode(Visual Studio Code下面简称VSCode)的插件 Setting ...

  4. 禅道设置bug模板_一款热度很高的项目管理和bug工具,免费使用,可在公司推广哦...

    以前在公司会用到各种bug管理工具,但使用最顺手的感觉还是禅道,主要是它除了能满足我的日常工作之外,用户体验上也做的不错 .前段时间领导碰巧看到了工具,觉得使用它管理项目应该不错,打算在全公司推广,让 ...

  5. 困扰一周的奇葩bug:重复相似代码多,导致单片机程序跑飞

    今天是个好日子,困扰一周的bug终于解决了,迫不及待将这个奇葩问题分享给各位朋友~ 硬件环境: 国产MCU:华大HC32L130 问题描述: 最近做一款基于Modbus协议的三通道温度采集模块,程序设 ...

  6. java string 占位符_驳《阿里「Java开发手册」中的1个bug》?

    前两天写了一篇关于<阿里Java开发手册中的 1 个bug>的文章,评论区有点炸锅了,基本分为两派,支持老王的和质疑老王的. 首先来说,无论是那一方,我都真诚的感谢你们.特别是「二师兄」, ...

  7. 开发人员绩效考核中有效bug数的统计

    我们都知道,开发人员的考核中,bug这块占了一定的比重,那么我们在统计每个开发人员的bug数时,显然要做到有效,不能把缺陷管理系统上的bug不经过处理,就直接进行统计. 如何统计有效bug数呢? 我们 ...

  8. 一个GDIPlus的Bug -- OutofMemory异常

    今天发现 framework2.0中的一个GDIPlus的Bug: 在Form的OnPaint事件里面写如下代码: private void Form1_Paint(object sender, Pa ...

  9. 从难免的线上bug说起代码的思考

    经常是某司线上又出bug了,然后是给公司造成多少损失,追根究底总是可以找到一些原因,诸如:写代码逻辑考虑不全面,或者代码有硬伤,也有测试不充分,甚至不测试都有,也有是运维的问题等等. 我对测试部专业, ...

最新文章

  1. C++中的命名空间namespace
  2. 『非常重要』非矿工用户如何安全度过BCH11月算力战历史时刻!
  3. POI各Jar包的作用
  4. 《系统集成项目管理工程师》必背100个知识点-72配置管理的主要活动
  5. filebeat + logstash 发送日志至kafka 入门
  6. 思考一下http.ListenAndServe + echo+gorm+xorm的可行性?
  7. python输入正整数n、求n以内能被17整除的最大正整数_求100之内自然数中最大的能被17整除的数资料...
  8. Linux虚拟内存管理 | 虚拟地址与物理地址映射、段错误SIGSEGV
  9. Oracle登陆SQL Plus,Oracle基础学习登陆SQLPLUS(一)
  10. 剑指offer(数值的整数次方)
  11. Java命令注入之防护
  12. 为何高端FPGA都非常重视软件
  13. Python实战题 · 计算圆面积
  14. html中背景渐变斜着渐变,CSS3 斜向渐变背景
  15. 浅谈扫描二维码登录微信网页版与摇一摇传图的实现原理
  16. 跨平台移动开发工具:PhoneGap与Titanium全方位比拼
  17. 基于opencv实现桌面图标识别
  18. 实战:战狼2票房数据分析——(3)数据读取及分析
  19. MFC中使用sqlite3操作数据库 创建,插入数据,查询数据
  20. MATLAB——Simulink如何将模块进行封装

热门文章

  1. 小学美术成功上岸备考经验心得
  2. 8个提供免费高品质的照片网站
  3. AutoCAD一维码、二维码的生成及使用,含Data Matrix,QRCode二维码、EAN-13条形码
  4. 园区基础网络配置系列——华三无线AC
  5. 企业数据存储方式发展趋势:数据仓库-大数据平台-数据湖-湖仓一体
  6. windows杀掉进程方法
  7. 【其他】游戏理论研究
  8. mysql中存储过程 解决参数作为表名
  9. 《充分利用你的24小时》读后感
  10. 云顶之弈阵容助手-基于遗传算法