gpt4 book ai didi

java - 使用 ISO 字符集压缩文件时线程 "main"java.util.zip.ZipException : Not in GZIP format. 中的异常

转载 作者:行者123 更新时间:2023-11-30 11:13:09 24 4
gpt4 key购买 nike

我在我的程序中尝试使用 GZIP 流压缩/解压缩数据,当使用字符集“ISO-8859-1”时,一切正常,但当将字符集更改为“UTF-8”时,我得到错误消息“线程“主”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返回,例如:

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

在decompress()中将String解码为bytes返回,如:

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

关于java - 使用 ISO 字符集压缩文件时线程 "main"java.util.zip.ZipException : Not in GZIP format. 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26528256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com