程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

线程“main”中的异常 java.util.zip.ZipException: 不是 GZIP 格式。使用 ISO 字符集压缩文件时

发布于2021-12-27 23:17     阅读(1226)     评论(0)     点赞(27)     收藏(5)


我正在我的程序中尝试使用 GZIP 流压缩/解压缩数据,当使用字符集“ISO-8859-1”时,一切正常,但是当将字符集更改为“UTF-8”时,我收到错误消息“线程“main”java.util.zip.ZipException 中的异常:不是 GZIP 格式”。这是我的代码:

public static String compress(String str) throws IOException { 
       if (str == null || str.length() == 0) { 
           return str; 
       } 
       System.out.println("String length : " + str.length()); 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       GZIPOutputStream gzip = new GZIPOutputStream(out); 
       gzip.write(str.getBytes()); 
       gzip.close(); 
       String outStr = out.toString("UTF-8"); 
       System.out.println("Output String lenght : " + outStr.length()); 
       System.out.println("Output : " + outStr.toString()); 
       return outStr; 
} 

public static String decompress(String str) throws IOException { 
       if (str == null || str.length() == 0) { 
          return str; 
       } 
       System.out.println("Input String length : " + str.length()); 
       GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str.getBytes("UTF-8"))); 
       BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8")); 
       String outStr = ""; 
       String line; 
       while ((line=bf.readLine())!=null) { 
             outStr += line; 
       } 
       System.out.println("Output String lenght : " + outStr.length()); 
       return outStr; 
} 

public static void main(String[] args) throws IOException { 
       String string = "my data"; 
       System.out.println("after compress:"); 
       String compressed = compress(string); 
       System.out.println(compressed); 
       System.out.println("after decompress:"); 
       String decomp = decompress(compressed); 
       System.out.println(decomp); 
} 

解决方案


String outStr = out.toString("UTF-8"); 这个“输出”是压缩字节流,将其编码为字符串,然后从字符串中解码会丢失一些字节。这可能是java的一个错误。要解决它,您可以在 compress() 中将字节编码为字符串以返回,例如:

String infoBase64Encode = new String(Base64.encodeBase64(out.toByteArray()))

将字符串解码为 decompress() 中的字节以返回,例如:

String infoBase64Decode = Base64.decodeBase64(decryptAESinfo)

完整代码如下:

public static String compress(String str) throws IOException { 
           if (str == null || str.length() == 0) { 
               return str; 
           } 
           System.out.println("String length : " + str.length()); 
           ByteArrayOutputStream out = new ByteArrayOutputStream(); 
           GZIPOutputStream gzip = new GZIPOutputStream(out); 
           gzip.write(str.getBytes()); 
           gzip.close(); 
           String outStr = new String(Base64.encodeBase64(out.toByteArray()));
           System.out.println("Output String lenght : " + outStr.length()); 
           System.out.println("Output : " + outStr.toString()); 
           return outStr; 
    } 

    public static String decompress(String str) throws IOException { 
           if (str == null || str.length() == 0) { 
              return str; 
           } 
           System.out.println("Input String length : " + str.length()); 
           GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeBase64(str))); 
           String outStr = ""; 
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           byte[] buffer = new byte[256];
           int n;
           while ((n = gis.read(buffer)) >= 0) {
               out.write(buffer, 0, n);
           }
           System.out.println("Output String lenght : " + outStr.length()); 
           return new String(out.toByteArray()); 
    } 

    public static void main(String[] args) throws IOException { 
           String string = "my data"; 
           System.out.println("after compress:"); 
           String compressed = compress(string); 
           System.out.println(compressed); 
           System.out.println("after decompress:"); 
           String decomp = decompress(compressed); 
           System.out.println(decomp); 
    } 


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/368169/d5adfc7a242000780c04/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)