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;
        }
    }
}
相关推荐
海兰13 小时前
【 Python 量化交易】第10章:八大经典策略
android·python·kotlin
Rytter13 小时前
Android诈骗裸聊软件逆向分析
android
zww894911113 小时前
外卖CPS小程序系统:从返利链路到订单回填的技术实现
小程序
lhldsg13 小时前
酒吧点餐小程序开发:从需求分析到技术落地实战
java·前端·小程序·架构·交友
JMchen12313 小时前
【Android 性能优化实战 60 讲】04 Memory Profiler 高阶玩法:深剖堆内存原理,实战定位 Kotlin 闭包隐式泄漏
android·性能优化·kotlin·实战·源码分析·内存泄漏·memory profiler
Android-Flutter13 小时前
Java线程池 - 内部线程管理机制详解
android
开开心心就好14 小时前
手机悬屏翻译工具外语游戏漫画APP全覆盖
android·前端·javascript·python·游戏·pdf·html
恋猫de小郭14 小时前
AI 时代,也许你的 Flutter 需要一套 Dartastic OpenTelemetry 监控
android·前端·flutter
sunphp开发者14 小时前
明灯智慧地图导览系统制作教程
vue·php·uniapp·js·贴图
Kapaseker14 小时前
我为什么喜欢 Compose ?
android·kotlin