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;
        }
    }
}
相关推荐
安东尼肉店2 小时前
Android compose屏幕适配终极解决方案
android
2501_916007472 小时前
HTTPS 抓包乱码怎么办?原因剖析、排查步骤与实战工具对策(HTTPS 抓包乱码、gzipbrotli、TLS 解密、iOS 抓包)
android·ios·小程序·https·uni-app·iphone·webview
feiyangqingyun4 小时前
基于Qt和FFmpeg的安卓监控模拟器/手机摄像头模拟成onvif和28181设备
android·qt·ffmpeg
用户2018792831678 小时前
ANR之RenderThread不可中断睡眠state=D
android
煤球王子8 小时前
简单学:Android14中的Bluetooth—PBAP下载
android
小趴菜82278 小时前
安卓接入Max广告源
android
齊家治國平天下8 小时前
Android 14 系统 ANR (Application Not Responding) 深度分析与解决指南
android·anr
ZHANG13HAO8 小时前
Android 13.0 Framework 实现应用通知使用权默认开启的技术指南
android
【ql君】qlexcel8 小时前
Android 安卓RIL介绍
android·安卓·ril
2501_916008898 小时前
HTTPS 双向认证抓包实战,原理、难点、工具与可操作的排查流程
网络协议·http·ios·小程序·https·uni-app·iphone