博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Deflater 压缩解压
阅读量:6927 次
发布时间:2019-06-27

本文共 1915 字,大约阅读时间需要 6 分钟。

import java.util.Arrays;import java.util.zip.Deflater;import java.util.zip.Inflater;import cc.zeelan.mall.common.assertion.Assert;/** * 数据压缩解压 *  * @project common-utils * @fileName StringZlibUtil.java * @Description * @author light-zhang * @date 2019年5月9日 * @version 1.0.0 */public class CompressUtil {    /**     * 压缩     *      * @param input       * @return     */    public static byte[] compress(byte[] input) {        try {            byte[] output = new byte[Integer.sum(input.length + 10,                    Double.valueOf(Math.ceil(input.length * 0.25f)).intValue())];            Deflater compresser = new Deflater(9);//压缩级别            compresser.setInput(input);            compresser.finish();            int compressedDataLength = compresser.deflate(output);            compresser.end();            return Arrays.copyOf(output, compressedDataLength);        } catch (Exception e) {            Assert.RuntimeException("数据压缩失败");        }        return null;    }    /**     * 解压缩     *      * @param barr 须要解压缩的字节数组     * @return     * @throws Exception     */    public static byte[] uncompress(byte[] barr) {        try {            byte[] result = new byte[2014];            Inflater inf = new Inflater();            inf.setInput(barr);            int infLen = inf.inflate(result);            inf.end();            return Arrays.copyOf(result, infLen);        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public static void main(String[] args) throws Exception {        String str = "abcde|qqqqqqqqqqqqqqq|wwwwwwwwwwwwwwwwwwww";         System.out.println("压缩前 " + str.getBytes().length);        byte[] def = CompressUtil.compress(str.getBytes());        System.out.println("压缩后 " + def.length);        byte[] inf = CompressUtil.uncompress(def);        System.out.println(new String(inf));     }}

 

转载于:https://www.cnblogs.com/light-zhang/p/10895810.html

你可能感兴趣的文章
10分钟上手最新vue脚手架
查看>>
flutter笔记2:android studio、android SDK、安卓虚拟机和运行第一个项目
查看>>
90%面试都不会问的题,因为...
查看>>
可拖动table表头的实现
查看>>
Vue学习日记(二)——Vue核心思想
查看>>
2018年第三周-计算机概论-操作系统篇-历史章节
查看>>
vhr部门管理数据库设计与编程
查看>>
[WUST2017]一组简单一点的题目(一)V - u Calculate e
查看>>
WEB前端面试题汇总整理02
查看>>
小聊BFC
查看>>
Ceph 编译(Giant版本)
查看>>
10位技术领袖告诉你趟过的微服务那些坑和最佳实践
查看>>
hystrix基础入门
查看>>
【288天】跃迁之路——程序员高效学习方法论探索系列(实验阶段46-2017.11.20)...
查看>>
简单说 通过JS的隐式转换,关键时刻救你一命
查看>>
node和vue-cli构建项目时安装的常用依赖
查看>>
【262天】我爱刷题系列(21)
查看>>
初探分布式系统之数据拆分
查看>>
安卓实战项目-动态桌面-rxjava实现搜索本地所有视频
查看>>
B+树的正确姿势
查看>>