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;
        }
    }
}
相关推荐
独角鲸网络安全实验室1 天前
2026微信小程序抓包全解析:从实操落地到合规风控,解锁前端调试新范式
前端·微信小程序·小程序·抓包·系统代理绕过·https证书严格校验·进程隔离
饭小猿人1 天前
Android 腾讯X5WebView如何禁止系统自带剪切板和自定义剪切板视图
android·java
_李小白1 天前
【android opencv学习笔记】Day 8: remap(像素位置重映射)
android·opencv·学习
美狐美颜SDK开放平台1 天前
多场景美颜SDK解决方案:直播APP(iOS/安卓)开发接入详解
android·人工智能·ios·音视频·美颜sdk·第三方美颜sdk·短视频美颜sdk
嗷o嗷o1 天前
Android BLE 里,MTU、分包和长数据发送到底该怎么处理
android
Gary Studio1 天前
Android AIDL HAL工程结构示例
android
y = xⁿ1 天前
MySQL八股知识合集
android·mysql·adb
andr_gale1 天前
04_rc文件语法规则
android·framework·aosp
祖国的好青年1 天前
VS Code 搭建 React Native 开发环境(Windows 实战指南)
android·windows·react native·react.js