caosouseyi 2016-08-31 03:26 采纳率: 16.7%
浏览 2347

java GZIP的压缩解压缩问题

public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
gzip.finish();
// return out.toString("ISO-8859-1");
return out.toString("UTF-8");
}

// 解压缩   
  public static String uncompress(String str) throws IOException {   
    if (str == null || str.length() == 0) {   
      return str;   
    }   
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("utf-8"));   
    GZIPInputStream gunzip = new GZIPInputStream(in);   
    byte[] buffer = new byte[256];   
    int n;   
    while ((n = gunzip.read(buffer))>=0) {   
      out.write(buffer, 0, n);   
    }   
    // toString()使用平台默认编码,也可以显式的指定如toString("GBK")  
    return out.toString();   
  }   

    Exception in thread "main" java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:141)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:56)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:65)
at Util.ZipUtil2.uncompress(ZipUtil2.java:106)
at Util.ZipUtil2.main(ZipUtil2.java:126)
  • 写回答

2条回答 默认 最新

  • devine007 2016-08-31 07:24
    关注

    public static byte[] compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
    return null;
    }
    byte[] outPut = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    gzip.finish();
    outPut = out.toByteArray();
    // return out.toString("ISO-8859-1");
    return outPut;
    }

    // 解压缩
    public static String uncompress(byte[] str) throws IOException {
    
        ByteArrayInputStream in = new ByteArrayInputStream(str);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码,也可以显式的指定如toString(&quot;GBK&quot;)
        return out.toString();
    }
    
    public static void main(String[] args) {
        try {
            byte[] b=Test.compress("111111111111");
            System.out.println(Test.uncompress(b));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        };
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名