嗨,我正在使用 GZIP 压缩和解压缩字符串。
得到一些例外!请帮帮我!
protected byte[] CompressInputString(String input_string2)
throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(
input_string2.length());
System.out.println("Byte Array OS : " + os);
GZIPOutputStream gos = new GZIPOutputStream(os);
System.out.println("GZIPOutputStream : " + gos);
gos.write(input_string2.getBytes());
System.out.println("GZIPOutputStream get bytes: "
+ input_string2.getBytes());
gos.close();
byte[] compressed = os.toByteArray();
os.close();
System.out.println("Compressed : " + compressed);
return compressed;
}
protected String DecompressInputString(byte[] input_to_decode_from_function)
throws IOException {
final int BUFFER_SIZE = 32;
ByteArrayInputStream is = new ByteArrayInputStream(input_to_decode_from_function);
GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) {
string.append(new String(data, 0, bytesRead));
}
gis.close();
is.close();
return string.toString();
}
我的输入是:abcdefghijklmnop
输出是
GZIPOutputStream : java.util.zip.GZIPOutputStream@f38798
GZIPOutputStream get bytes: [B@4b222f
Compressed : [B@b169f8
Compressed File : [B@b169f8
要解压缩,我应该提供什么输入?
如果我将[B@b169f8
其作为字符串输入并使用 input.getBytes() 将其转换为字节数组并将其传递给我的解压缩函数,则会引发异常