47

次のコードを使用して文字列データを圧縮および解凍していますが、直面している問題は、エラーなしで簡単に圧縮されることですが、解凍メソッドは次のエラーをスローします。

スレッド「メイン」での例外 java.io.IOException: Not in GZIP format

public static void main(String[] args) throws Exception {
        String string = "I am what I am hhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
                + "bjggujhhhhhhhhh"
                + "rggggggggggggggggggggggggg"
                + "esfffffffffffffffffffffffffffffff"
                + "esffffffffffffffffffffffffffffffff"
                + "esfekfgy enter code here`etd`enter code here wdd"
                + "heljwidgutwdbwdq8d"
                + "skdfgysrdsdnjsvfyekbdsgcu"
                +"jbujsbjvugsduddbdj";

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


     public static String compress(String str) throws Exception {
        if (str == null || str.length() == 0) {
            return str;
        }
        System.out.println("String length : " + str.length());
        ByteArrayOutputStream obj=new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(obj);
        gzip.write(str.getBytes("UTF-8"));
        gzip.close();
        String outStr = obj.toString("UTF-8");
        System.out.println("Output String length : " + outStr.length());
        return outStr;
     }

      public static String decompress(String str) throws Exception {
        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;
     }

この問題を解決する方法がまだわかりません!!!

4

7 に答える 7

45

これは

String outStr = obj.toString("UTF-8");

byte[]から取得できる を送信し、それをByteArrayOutputStreamそのまま使用してByteArrayInputStreamを構築しますGZIPInputStream。以下は、コードで行う必要がある変更です。

byte[] compressed = compress(string); //In the main method

public static byte[] compress(String str) throws Exception {
    ...
    ...
    return obj.toByteArray();
}

public static String decompress(byte[] bytes) throws Exception {
    ...
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
    ...
}
于 2013-05-03T04:51:13.473 に答える
14

圧縮されたコンテンツをネットワーク経由で転送したり、テキストとして保存したりする必要がある場合は、Base64 エンコーダー (apache commons codec Base64 など) を使用してバイト配列を Base64 文字列に変換し、文字列をバイト配列にデコードする必要があります。リモート クライアント。Use Zip Stream and Base64 Encoder to Compress Large String Dataで例を見つけました!

于 2013-11-13T02:55:00.440 に答える
7

問題は次の行です。

    String outStr = obj.toString("UTF-8");

バイト配列objには、任意のバイナリ データが含まれます。任意のバイナリ データを UTF-8 であるかのように "デコード" することはできません。試してみると、バイトに「エンコード」できない文字列が得られます。または、少なくとも、取得するバイトは最初のバイトとは異なります...それらが有効な GZIP ストリームでなくなる程度まで。

修正は、バイト配列の内容をそのまま保存または送信することです。文字列に変換しようとしないでください。テキストではなくバイナリデータです。

于 2013-05-03T04:48:26.840 に答える