基于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++
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
11614 4
|
SQL 监控 druid
Druid未授权访问 漏洞复现
Druid未授权访问 漏洞复现
22528 1
|
存储 监控 安全
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
1532 0
|
Java Swift Android开发
Kotlin vs Swift:现代移动开发的“双子星”全面对比
Kotlin与Swift是现代移动开发的两大主流语言,分别主导Android与iOS生态。本文全面对比二者在语法、空安全、并发、性能及跨平台等方面的异同,深入解析其设计理念与适用场景,助你根据平台需求与技术战略做出最优选择。
985 0
|
Ubuntu Linux Shell
Ubuntu GRUB菜单密码重置教程
本文详细介绍了在Ubuntu 16.04系统中通过GRUB菜单找回密码的方法。包括进入GRUB引导菜单、修改内核参数、重置用户密码及完成重启的完整步骤,帮助用户快速恢复系统访问权限。
1319 0
|
JavaScript 前端开发
在Vue3+ElementPlus项目中实现一个简单的新增/移除行记录的小组件
在Vue 3和Element Plus项目中创建一个支持新增和移除行记录的简单表格组件。
1241 10
[UE 虚幻引擎] DTLoadFbx 运行时加载FBX本地模型插件说明
该插件支持在运行时动态加载FBX模型,无需预先打包。通过新建Actor并添加DT Runtime Fbx Component,然后调用LoadFile函数加载模型路径(不支持动画)。加载时可选择是否创建碰撞体,该组件基于UProceduralMeshComponent,提供与PMC相似的设置。启用异步计算(Use Async Cooking)可加速碰撞体生成。
1122 0
|
安全 Java 网络安全
RestTemplate进行https请求时适配信任证书
RestTemplate进行https请求时适配信任证书
1560 3