嗨我正在使用GZIP壓縮和解壓縮字符串。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 : [email protected]
GZIPOutputStream get bytes: [[email protected]
Compressed : [[email protected]
Compressed File : [[email protected]
要解壓縮了,我給什麼輸入?
如果我把[[email protected]
作爲字符串輸入和使用input.getBytes(其轉換爲字節陣列)並將其傳遞到我的解壓縮功能,將引發一個例外
第一個建議:*從不*使用'String.getBytes()'而不指定編碼... –
那麼第二個選項是什麼先生,我需要儘快得到這個代碼! –
這不是一個「選項」 - 它只是錯誤的一部分。看到我的答案更多。 –