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 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿2 天前
Android MediaPlayer 笔记
android
Jony_2 天前
Android 启动优化方案
android
阿巴斯甜2 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇2 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android