2016-04-14 12 views
0

文字列を圧縮しようとしています。私はBase64のエンコーディングとデコードを使用して、StringからBytesおよびviceversaへの変換を行っています。圧縮とエンコーディングが文字列で間違った結果を返す

import org.apache.axis.encoding.Base64; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.util.zip.Deflater; 
import java.util.zip.Inflater; 

public class UtilTesting { 
    public static void main(String[] args) { 


     try { 
      String original = "I am the god"; 
      System.out.println("Starting Zlib"); 
      System.out.println("=================================="); 
      String zcompressed = compressString(original); 
      String zdecompressed = decompressString(zcompressed); 
      System.out.println("Original String: "+original); 
      System.out.println("Compressed String: "+zcompressed); 
      System.out.println("Decompressed String: "+zdecompressed); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    public static String compressString(String uncompressedString){ 
     String compressedString = null; 
     byte[] bytes = Base64.decode(uncompressedString); 
     try { 
      bytes = compressBytes(bytes); 
      compressedString = Base64.encode(bytes); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return compressedString; 
    } 

    public static String decompressString(String compressedString){ 
     String decompressedString = null; 
     byte[] bytes = Base64.decode(compressedString); 
     try { 
      bytes = decompressBytes(bytes); 
      decompressedString = Base64.encode(bytes); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (DataFormatException e) { 
      e.printStackTrace(); 
     } 
     return decompressedString; 
    } 

    public static byte[] compressBytes(byte[] data) throws IOException { 
     Deflater deflater = new Deflater(); 
     deflater.setInput(data); 
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 
     deflater.finish(); 
     byte[] buffer = new byte[1024]; 
     while (!deflater.finished()) { 
      int count = deflater.deflate(buffer); // returns the generated code... index 
      outputStream.write(buffer, 0, count); 
     } 
     outputStream.close(); 
     byte[] output = outputStream.toByteArray(); 
     return output; 
    } 

    public static byte[] decompressBytes(byte[] data) throws IOException, DataFormatException { 
     Inflater inflater = new Inflater(); 
     inflater.setInput(data); 
     ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 
     byte[] buffer = new byte[1024]; 
     while (!inflater.finished()) { 
      int count = inflater.inflate(buffer); 
      outputStream.write(buffer, 0, count); 
     } 
     outputStream.close(); 
     byte[] output = outputStream.toByteArray(); 
     return output; 
    } 
} 

これが結果を与えている:あなたが見ることができるように

Starting Zlib 
================================== 
Original String: I am the god 
Compressed String: eJxTXLm29YUGAApUAw0= 
Decompressed String: Iamthego 

は、それはホワイトスペースが不足していると言っても与えられた文字列の最後の文字を失いました。

誰かがこのコードで何が問題なのかを教えてください。 は、私はステップの下に、次のよ:

  • デコード
  • 解凍
  • エンコードを取得
  • 保存

    1. デコード
    2. 湿布
    3. エンコード

    助けてください。ありがとうございました。 compressString

  • +0

    '新しいByteArrayOutputStream(data.length)' =>で、多分あなたはより大きなバッファを必要とします入力。 –

    +0

    @AndyTurnerそれを10倍に増やしました。違いはありませんでした。空白についての考え方は? –

    +0

    私は 'compressString'の' Base64.decode'へのあなたの最初の呼び出しについて疑念を持っています。あなたはBase64でエンコードされた文字列を渡していません。私が推測しなければならないのは、スペースが奪われた場所だと言えるだろう。 –

    答えて

    1

    、交換してください:

    uncompressString.getBytes(StandardCharsets.UTF_8) 
    

    Base64.decode(uncompressedString) 
    

    をあなたはbase64でエンコードされた文字列を渡していません。単純に入力文字列のバイトが必要です。空白はbase64エンコーディングでは決して現れないので、おそらく冗長とみなされ、破棄されることに注意してください。

    同様decompressStringで、交換してください:あなたはデータを解凍している場合

    Base64.encode(bytes) 
    

    new String(bytes, StandardCharsets.UTF_8) 
    
    +0

    @おめでとう、それは働いた。私は不必要にbase64を使って複雑にしました。 –

    関連する問題