2012-10-04 96 views
0

嗨我正在使用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(其轉換爲字節陣列)並將其傳遞到我的解壓縮功能,將引發一個例外

NOT IN GZIP FORMAL EXCEPTION LOG

+0

第一個建議:*從不*使用'String.getBytes()'而不指定編碼... –

+0

那麼第二個選項是什麼先生,我需要儘快得到這個代碼! –

+0

這不是一個「選項」 - 它只是錯誤的一部分。看到我的答案更多。 –

回答

3

如果我把[乙@ b169f8作爲字符串輸入,並使用INPUT.getBytes()將其轉換爲字節數組,並將它傳遞給我的解壓縮功能......一個異常引發

嗯,是 - 因爲「[B @ b169f8」 ISN」實際上字節數組的內容。這只是撥打toString()的結果;它基本上指示了對象的類型和它的散列。

如果要以無損方式將字節數組轉換爲字符串,則需要使用類似base64的東西。有很多執行base64編碼的庫,包括this public domain one

此外,我會強烈建議不要使用String.getBytes()String(byte[], int, int)構造函數。你應該總是指定一個編碼。讀取數據最簡單的方法是創建一個包含GZIPInputStreamInputStreamReader,並直接從中讀取文本數據。我建議使用UTF-8作爲初始文本到二進制轉換的編碼(當然也是在解壓方面)。

+0

你能否爲你的建議添加一個代碼,例如 - 壓縮和解壓縮功能,請 –

+0

@ rolling.stones:不,我恐怕沒有時間。我已經解釋了問題所在,並給出了指向base64編碼器/解碼器的指針。如果您有*特定*更多問題,我可以回答他們。 –

+0

@ Jon Skeet:你能否糾正我在下面的壓縮和解壓縮調用中添加的註釋中的錯誤!我的壓縮看起來很棒,我輸入了什麼,怎麼打電話來獲得輸出? –

0

首先,您將字節數組轉換爲字符串,使用.toString()字節數組,它返回其內存地址的表示,而不是其內容。

你會想要一個合理的方式來表示你的字節數組作爲一個字符串,如果你確實需要它作爲一個字符串之間。 Base64是一個合理的方法來做到這一點。

然後,您需要在將其饋送到充氣器之前將其重新轉換爲字節數組。或者,您可以跳過上述兩個步驟,只將字節數組保存爲字節數組,然後將其直接送入充氣機。

0

做工精細對我來說,我已經打過電話了你的方法,像這樣:

public static void main(String[] args) throws Throwable { 
    Test tf = new Test(); 
    byte[] compressed = tf.CompressInputString("Jimmy"); 
    System.out.println(Arrays.toString(compressed)); 
    String s = tf.DecompressInputString(compressed); 
    System.out.println(s);//Prints "Jimmy" 

} 

您可以添加如何調用的方法,以及任何異常的完整堆棧跟蹤的例子嗎?

+0

當然可以添加例子! –

+0

**解壓縮**'input_string_for_decode = input_to_decode.getText(); \t \t \t \t \t input_string_for_decode_bytes = input_string_for_decode \t \t \t \t \t \t \t .getBytes(); decompressed = DecompressInputString(input_string_for_decode_bytes); \t \t \t \t \t lblDecodedString.setText( 「解碼值:」 +減壓);' –

+0

** **壓縮' = input_string_for_encode input_to_encode.getText()的toString(); \t \t \t \t嘗試{ \t \t \t \t \t壓縮= CompressInputString(input_string_for_encode); \t \t \t \t \t lblEncodedString.setText( 「編碼值:」 \t \t \t \t \t \t \t + compressed.toString()); System.out.println(「壓縮文件:」+壓縮); \t \t \t \t \t input_to_decode.setText(compressed.toString());'' –