Java壓縮/解壓縮字符串


Java使用壓縮庫為常規壓縮提供了Deflater類。它還提供了DeflaterOutputStream,它使用Deflater類通過壓縮(壓縮)數據流,然后將壓縮后的數據寫入另一個輸出流來過濾數據流。有等效的InflaterInflaterOutputStream類來處理解壓。

壓縮

這是一個如何使用DeflatorOutputStream壓縮字節數組的示例。

    /**
     * 壓縮字符串,默認梳utf-8
     *
     * @param text
     * @return
     */
    public static String zipBase64(String text) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out)) {
                deflaterOutputStream.write(text.getBytes(Constant.UTF_8));
            }
            return new String(DecodeEncode.base64Encode(out.toByteArray()));
        } catch (IOException e) {
            logger.error("壓縮文本失敗:{}", text, e);
        }
        return EMPTY;
    }

讓我們測試一下:

    public static void main(String[] args) throws IOException {

        String text = DecodeEncode.zipBase64("5615616119688refdaf888888888888888865555555555555511111111111111111111111119999999999999999999999999999999911111111111111111111333333333333333333");
        output(text);

        String s = DecodeEncode.unzipBase64(text);
        output(s);

        output(text.length() + TAB + s.length());
    }

控制台輸出:

INFO-> eJwzNTM0NTM0MzS0NLOwKEpNS0lMs0ADZqYowBAXsCQAsOkxxgAALV0fBw==
INFO-> 5615616119688refdaf888888888888888865555555555555511111111111111111111111119999999999999999999999999999999911111111111111111111333333333333333333
INFO-> 60	145

解壓

    /**
     * 解壓字符串,默認utf-8
     *
     * @param text
     * @return
     */
    public static String unzipBase64(String text) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            try (OutputStream outputStream = new InflaterOutputStream(os)) {
                outputStream.write(DecodeEncode.base64Byte(text));
            }
            return new String(os.toByteArray(), Constant.UTF_8);
        } catch (IOException e) {
            logger.error("解壓文本失敗:{}", text, e);
        }
        return EMPTY;
    }

讓我們測試一下:

    public static void main(String[] args) throws IOException {
        String text = "eJwzNTM0NTM0MzS0NLOwKEpNS0lMs0ADZqYowBAXsCQAsOkxxgAALV0fBw==";
        String s = DecodeEncode.unzipBase64(text);
        output(s);
        output(text.length() + TAB + s.length());
    }

控制台輸出:


INFO-> 當前用戶:fv,IP:10.60.192.21,工作目錄:/Users/fv/Documents/workspace/fun/,系統編碼格式:UTF-8,系統Mac OS X版本:10.15.6
INFO-> 5615616119688refdaf888888888888888865555555555555511111111111111111111111119999999999999999999999999999999911111111111111111111333333333333333333
INFO-> 60	145

測試用例

用的是spock測試框架,這里用來驗證一下,壓縮后的字符串和壓縮前的長短。

參考文章:


     def "測試加密解密"() {

        expect:
        name.length() > DecodeEncode.zipBase64(name).length()

        where:
        name << ["00000000000000000000000000000000000000000000000000000",
                "51666666666666666666666666666666666666666666666666666",
                "(&%^&%*&%(^(^(*&^*(&^(*&^(*^(*&%^%^\$^%##@#!#@!~~#@",
                "發大房東放大反動發動機吧就產國產過高冬季佛冬季風戳分床三佛",
                 "gkjdgjdgjlfdjgldkgjfdsafoiwehoirehtoiewho"]
    }

測試結果:

Condition not satisfied:

name.length() > DecodeEncode.zipBase64(name).length()
|    |        | |            |         |     |
|    50       | |            |         |     64
|             | |            |         (&%^&%*&%(^(^(*&^*(&^(*&^(*^(*&%^%^$^%##@#!#@!~~#@
|             | |            eJwdxVsNADAMAkAtC4M0qKgSpFT7HrmPKzGixcplxaV/+cUwOwQaC71m0AcDsQqi
|             | class com.fun.utils.DecodeEncode
|             false
(&%^&%*&%(^(^(*&^*(&^(*&^(*^(*&%^%^$^%##@#!#@!~~#@


	at com.FunTester.spock.pratice.ZIP.測試加密解密(ZIP.groovy:29)


Condition not satisfied:

name.length() > DecodeEncode.zipBase64(name).length()
|    |        | |            |         |     |
|    29       | |            |         |     120
|             | |            |         發大房東放大反動發動機吧就產國產過高冬季佛冬季風戳分床三佛
|             | |            eJwlyEEKgCAURdHdmwllQYPAZgYKFRRNRDA3898nd5HQ5HK40CNWxzJTWHh6qqE7KI/6leclYnA4L4oOJtW+uSnbDLHjsJTMj2J7ljekQFQU2vo/xR8+gg==
|             | class com.fun.utils.DecodeEncode
|             false
發大房東放大反動發動機吧就產國產過高冬季佛冬季風戳分床三佛


	at com.FunTester.spock.pratice.ZIP.測試加密解密(ZIP.groovy:29)


Condition not satisfied:

name.length() > DecodeEncode.zipBase64(name).length()
|    |        | |            |         |     |
|    41       | |            |         |     60
|             | |            |         gkjdgjdgjlfdjgldkgjfdsafoiwehoirehtoiewho
|             | |            eJwVwgkKACAIBMC3BqvrBUIFfj8ahhngX4pgIRmKs7R9xNq32G2XsX5gTRDw
|             | class com.fun.utils.DecodeEncode
|             false
gkjdgjdgjlfdjgldkgjfdsafoiwehoirehtoiewho


	at com.FunTester.spock.pratice.ZIP.測試加密解密(ZIP.groovy:29)


其中兩個沒通過,感覺這個壓縮針對存數字的效果會比較好,或者把處理成byte[]會比較好用。網上看一些資料,主要還是用來壓縮文件的,有的看着效果還不錯,不過讓我想起來一個梗:壓縮完的文件大小大於壓縮前。


  • 公眾號FunTester首發,更多原創文章:FunTester430+原創文章,歡迎關注、交流,禁止第三方擅自轉載。

熱文精選


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM