java解压缩出现异常:Unexpected end of ZLIB input stream

vaintwyt 2014-04-18 12:11:15
想通过java原生API将字符串进行压缩和解压缩,参考使用了别人的代码后,报异常了(Unexpected end of ZLIB input stream)。

在解压缩函数的gunzip.read(buffer)出了问题,跟踪进去看,异常由InflaterInputStream类的fill函数抛出。
 
protected void fill() throws IOException {
ensureOpen();
len = in.read(buf, 0, buf.length);
if (len == -1) {
throw new EOFException("Unexpected end of ZLIB input stream");
}
inf.setInput(buf, 0, len);
}


百度和google了一轮,都没有找到解决方法。。。请求大神出手啊!

代码如下:



public static String GetCompress(String src)
{
if (src == null || src.isEmpty()) {
return src;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = null;
String des = null;
try {
gzip = new GZIPOutputStream(out);
gzip.write(src.getBytes());
//由于压缩后的数据需要传输,所以用了BASE64编码
des = new BASE64Encoder().encode(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(gzip!=null)
{
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return des;
}


public static String GetDeCompress(String src)
{
if (src == null || src.isEmpty()) {
return src;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPInputStream gunzip = null;
String des = null;
try {
ByteArrayInputStream in = new ByteArrayInputStream(
new BASE64Decoder().decodeBuffer(src));
gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int n;
n = gunzip.read(buffer);
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
des = out.toString();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(gunzip!=null)
{
try {
gunzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return des;
}
...全文
22059 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
banzhengyu 2016-03-22
  • 打赏
  • 举报
回复
楼主果然正解:确实是在使用gzipInputStream去是获得“输出流压缩的数据”之前,需要将gzipOutputStream关闭。
S_ShineBoy 2015-05-05
  • 打赏
  • 举报
回复
楼主正解,很好的解决了问题所在
vaintwyt 2014-05-06
  • 打赏
  • 举报
回复
隔了一段时间,再看看。终于解决了问题。 原来是获得输出流的数据之前,需要将gzip关闭。 另外,我上面说的压缩到16,是因为之前出现异常,所以才那么小。其实应该是8百多。 结帖啦

    public static String GetCompress(String src)
    {
        if (src == null || src.isEmpty()) {
                        return src;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        String des = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(src.getBytes());
                        //由于压缩后的数据需要传输,所以用了BASE64编码
            //des = new BASE64Encoder().encode(out.toByteArray()); //原先在这个获得压缩数据
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(gzip!=null)
            {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new BASE64Encoder().encode(out.toByteArray());//最终改成这里获得压缩数据
    }
vaintwyt 2014-04-18
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
压缩用Deflater类。
你好,我根据下面网址使用Deflater压缩,Inflater解压。没有出现问题。不过,原本1046长度的字符串,压缩后变成1144长。 而我用gzip的话,可以压缩到16。 http://navylee.iteye.com/blog/1661733
  • 打赏
  • 举报
回复
压缩用Deflater类。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧