发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!