簡體   English   中英

如何輕松地將字符串壓縮和解壓縮到字節數組?

[英]How can I easily compress and decompress Strings to/from byte arrays?

我有一些字符串,每個字符串大約10K字符。 它們有很多重復。 它們是序列化的JSON對象。 我想輕松地將它們壓縮成一個字節數組,並從字節數組中解壓縮它們。

我怎樣才能最輕松地做到這一點? 我正在尋找方法,所以我可以做以下事情:

String original = "....long string here with 10K characters...";
byte[] compressed = StringCompressor.compress(original);
String decompressed = StringCompressor.decompress(compressed);
assert(original.equals(decompressed);

你可以試試

enum StringCompressor {
    ;
    public static byte[] compress(String text) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            OutputStream out = new DeflaterOutputStream(baos);
            out.write(text.getBytes("UTF-8"));
            out.close();
        } catch (IOException e) {
            throw new AssertionError(e);
        }
        return baos.toByteArray();
    }

    public static String decompress(byte[] bytes) {
        InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[8192];
            int len;
            while((len = in.read(buffer))>0)
                baos.write(buffer, 0, len);
            return new String(baos.toByteArray(), "UTF-8");
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    }
}

使用這種不那么復雜的解壓縮函數代碼可以改進Peter Lawrey的答案

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        OutputStream out = new InflaterOutputStream(baos);
        out.write(bytes);
        out.close();
        return new String(baos.toByteArray(), "UTF-8");
    } catch (IOException e) {
        throw new AssertionError(e);
    }

我創建了一個庫來解決壓縮泛型字符串(特別是短字符串)的問題。 它嘗試使用各種算法壓縮字符串(普通utf-8,5位拉丁字母編碼,霍夫曼編碼,長字符串gzip)並選擇結果最短的算法(在最壞的情況下,它會選擇utf-8)編碼,這樣你就不會冒失去空間的風險)。

我希望它可能有用,這是鏈接https://github.com/lithedream/lithestring

編輯:我意識到你的字符串總是“長”,我的庫默認為gzip這些大小,我擔心我不能為你做得更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM