Exception in thread “main” java.util.zip.ZipException: Not in GZIP format

我试图在我的程序中使用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”); 这个“out”是ziped字节流,将其编码为String,然后从String解码它将丢失一些字节。这可能是java的一个bug。要解决它,你可以在compress()中将字节编码为String来返回,比如

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

将String解压缩()返回字节,如:

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);
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值