zoukankan      html  css  js  c++  java
  • 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+原创文章,欢迎关注、交流,禁止第三方擅自转载。

    热文精选

  • 相关阅读:
    【转】Unity学习笔记——MonoBehaviour类Invoke, Coroutine
    【转】Unity3D协同程序(Coroutine)
    Java定时任务
    SMSLib开发指南
    短信猫JAVA二次开发包SMSLib,org.smslib.TimeoutException: No response from device解决方案
    注解+反射+JDBC,实现一个简易的泛型DAO接口
    利用Window超级终端,修改短信猫波特率
    深入理解JDK动态代理机制
    深入分析Java ClassLoader原理
    Linux内核升级指南
  • 原文地址:https://www.cnblogs.com/FunTester/p/13408292.html
Copyright © 2011-2022 走看看