基于zlibCompress + Base64实现字符串与二维码的生成和恢复

简介: 文章介绍了如何使用zlib进行数据压缩和解压缩,以及如何结合Base64编码解码来生成和恢复包含压缩数据的二维码。

一、zlibCompress数据压缩

public static byte[] zlibCompress(byte[] data) {
    byte[] output;

    //LEVEL 5 to make it the same as iOS COMPRESSION_ZLIB
    Deflater compresser = new Deflater(5,true);

    compresser.reset();
    compresser.setInput(data);
    compresser.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!compresser.finished()) {
            int i = compresser.deflate(buf);
            bos.write(buf, 0, i);
        }
        output = bos.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    compresser.end();
    return output;
}

二、zlibCompress数据解压缩

public static byte[] zlibDecompress(byte[] data) {
    byte[] output;

    Inflater decompresser = new Inflater(true);
    decompresser.reset();
    decompresser.setInput(data);

    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;
}

三、二维码的生成和恢复

//decompress

String str = result.substring(AppConstants.WatchWallet.PREFIX_SIGN.length());

byte[] decodedBytes = android.util.Base64.decode(str.getBytes("UTF-8"), Base64.NO_WRAP);
qrstr_decoded = new String(StringUtils.zlibDecompress(decodedBytes));


//compress
byte[] encodedBytes = Base64.encode(StringUtils.zlibCompress(jsonstr.getBytes("utf-8")), Base64.NO_WRAP);
mQrcodeMvpPresenter.createQrcode(AppConstants.WatchWallet.PREFIX_SIGN + new String(encodedBytes));

四、代码下载

点击下载

相关文章
|
存储 监控 安全
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
1500 0
|
12月前
|
Java Swift Android开发
Kotlin vs Swift:现代移动开发的“双子星”全面对比
Kotlin与Swift是现代移动开发的两大主流语言,分别主导Android与iOS生态。本文全面对比二者在语法、空安全、并发、性能及跨平台等方面的异同,深入解析其设计理念与适用场景,助你根据平台需求与技术战略做出最优选择。
914 0
|
存储 缓存 JSON
详解HTTP四种请求:POST、GET、DELETE、PUT
【4月更文挑战第3天】
78087 5
详解HTTP四种请求:POST、GET、DELETE、PUT
|
Ubuntu Linux Shell
Ubuntu GRUB菜单密码重置教程
本文详细介绍了在Ubuntu 16.04系统中通过GRUB菜单找回密码的方法。包括进入GRUB引导菜单、修改内核参数、重置用户密码及完成重启的完整步骤,帮助用户快速恢复系统访问权限。
1270 0
|
安全 Linux 开发工具
【小白必看】GitKraken超详细图文安装教程(附官方下载链接)
GitKraken 是一款跨平台的 Git 客户端,提供直观界面简化版本控制。支持 Windows、Mac 和 Linux,适合个人与团队使用。本文详细介绍其下载、安装流程及配置方法,包括语言设置、Git 配置、多分支管理等功能,并解答常见问题,助你高效管理代码仓库。
|
JavaScript 前端开发
在Vue3+ElementPlus项目中实现一个简单的新增/移除行记录的小组件
在Vue 3和Element Plus项目中创建一个支持新增和移除行记录的简单表格组件。
1228 10
|
安全 网络安全 Apache
CVE-2021-41773 复现
CVE-2021-41773 复现
1396 1
|
安全 Java 网络安全
RestTemplate进行https请求时适配信任证书
RestTemplate进行https请求时适配信任证书
1529 3