我试图在我的程序中使用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);
}