Android 实现zlib解压

来源:互联网 发布:ubuntu ed2k下载工具 编辑:程序博客网 时间:2024/05/02 22:34

清明时节雨纷纷。
在这四月的第一天;在这繁花似锦,万物复苏的月份,写下我这月的第一篇博文。
希望能给后面需要用到的小伙伴提供指引,少走弯路。
刚好项目需要做大量数据的查询处理,因此使用到了此解压缩技术,废话不多说,
直接上代码。

        //定义byte数组用来放置解压后的数据        byte[] output = new byte[0];        Inflater decompresser = new Inflater();        decompresser.reset();        //设置当前输入解压        decompresser.setInput(data, 0, data.length);        ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);        try {            byte[] buf = new byte[1024];            while (!decompresser.finished()) {                int i = decompresser.inflate(buf);                o.write(buf, 0, i);            }            output = o.toByteArray();        } catch (Exception e) {            output = data;            e.printStackTrace();        } finally {            try {                o.close();            } catch (IOException e) {                e.printStackTrace();            }        }        decompresser.end();        return output;
1 0