GZip+Base64压缩字符串在ios上解压报错问题解决(安卓、PC模拟器正常)

java这边的压缩代码

引入的是java8 jdk自带的gzip压缩( java.util.zip.GZIPOutputStream)、BASE64Encoder( sun.misc.BASE64Encoder)

java 复制代码
public static String compress(String str) {
        if (str != null && str.length() != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = null;

            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(str.getBytes());
            } catch (IOException e) {
                log.error("字符串压缩异常!", e);
                e.printStackTrace();
            } finally {
                IoUtil.close(gzip);
            }

            return (new BASE64Encoder()).encode(out.toByteArray());
        } else {
            return str;
        }
    }

小程序的解压代码

js 复制代码
	let pakoContent =  uni.base64ToArrayBuffer(content);
	pakoContent = pako.ungzip(pakoContent, { to: 'string' });
	return pakoContent

被压缩字符串

text 复制代码
物流行业如何应对全球化供应链的挑战?

ios报错信息

aotb failed : invalid string length 105

经过测试,java端压缩出的结果长度有106,不是4的整数倍

自行查看没有发现明显问题后询问AI查找可能原因

经过验证,确实sum.misc的base64有问题,更换为java.util下的解决了

修改后的

java 复制代码
import cn.hutool.core.io.IoUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringCompressUtils {
    private static final Logger log = LoggerFactory.getLogger(StringCompressUtils.class);

    public StringCompressUtils() {
    }

    public static String compress(String str) {
        if (str != null && str.length() != 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = null;

            try {
                gzip = new GZIPOutputStream(out);
                gzip.write(str.getBytes());
            } catch (IOException e) {
                log.error("字符串压缩异常!", e);
                e.printStackTrace();
            } finally {
                IoUtil.close(gzip);
            }

            return Base64.getEncoder().encodeToString(out.toByteArray());
        } else {
            return str;
        }
    }

    public static String uncompress(String compressedStr) {
        if (compressedStr == null) {
            return null;
        } else {
            byte[] compressed = null;
            String decompressed = null;
            GZIPInputStream ginzip = null;
            ByteArrayInputStream in = null;
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            try {
                compressed = Base64.getDecoder().decode(compressedStr);
                in = new ByteArrayInputStream(compressed);
                ginzip = new GZIPInputStream(in);
                byte[] buffer = new byte[1024];
                int offset = -1;

                while((offset = ginzip.read(buffer)) != -1) {
                    out.write(buffer, 0, offset);
                }

                decompressed = out.toString();
            } catch (IOException e) {
                log.error("字符串解压缩异常!", e);
                e.printStackTrace();
            } finally {
                IoUtil.close(ginzip);
                IoUtil.close(in);
                IoUtil.close(out);
            }

            return decompressed;
        }
    }
}
相关推荐
00后程序员张11 小时前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
悠哉清闲13 小时前
不同车型drawable不同
android·开发语言
郑州光合科技余经理15 小时前
技术架构:海外版外卖平台搭建全攻略
java·大数据·人工智能·后端·小程序·架构·php
00后程序员张16 小时前
在 iOS 设备上同时监控 CPU、GPU 与内存的方法
android·ios·小程序·https·uni-app·iphone·webview
测试_AI_一辰16 小时前
项目实践笔记 9:打卡/日报Agent项目Bug 修改与稳定性收口(v1.0)
android·开发语言·人工智能·功能测试·ai编程·ab测试
马 孔 多 在下雨16 小时前
Kotlin协程进阶王炸之作-Kotlin的协程到底是什么
android·开发语言·kotlin
冬奇Lab16 小时前
【Kotlin系列15】多平台开发实战:一次编写,多端运行
android·开发语言·kotlin
Dxy123931021616 小时前
告别默认排序:MySQL自定义排序的“炼金术”
android·数据库·mysql
请叫我大虾17 小时前
发现一个jdk中ArrayList的小BUG
android·java·bug
一起养小猫17 小时前
Flutter for OpenHarmony 实战:双控制系统实现(按钮+键盘)
android·flutter·计算机外设·harmonyos