gpt4 book ai didi

java - 使用 Java.util.zip.Inflater 解压缩字符串

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:14:50 24 4
gpt4 key购买 nike

我正在尝试解码我​​收到的字符串。它是使用 deflater 压缩的:https://github.com/dankogai/js-deflate然后进行base64编码。

但是,当我使用 java inflater 时,我收到以下错误消息:未知的压缩方法。

    import sun.misc.BASE64Decoder;

public void org() throws Exception{
BASE64Decoder decoder = new BASE64Decoder();

try {

String inputString = "84VWAVDY";
byte[] decodedByteArray = decoder.decodeBuffer(inputString);

// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(decodedByteArray);
byte[] result = new byte[100];

int resultLength = decompresser.inflate(result);
decompresser.end();

// Decode the bytes into a String
String outputString = new String(result, 0, resultLength);
System.out.println("OUTPUT:" + outputString);

} catch (Exception e){
System.out.println("Exception: " + e);
}
}

此代码基本上是 Java API 的复制/粘贴。我也尝试过使用新的 Inflater(true);即 nowrap

“注意:使用‘nowrap’选项时,还需要提供一个额外的“虚拟”字节作为输入。ZLIB native 库需要这样做才能支持某些优化。”

那么这个虚拟字节应该添加到哪里呢?在“byte[] decodedByteArray”的开头或结尾?

那么有什么想法可以解决这个问题吗?我是否只需要添加虚拟字节,是否需要使用其他一些方法等?

好吧,我想就是这样,感谢所有帮助!

问候

约翰

最佳答案

将在末尾添加虚拟字节。然而,只有 zlib 1.1.4 及更早版本才需要它。当前版本的 zlib 不需要它。我不确定 java.util.zip 使用的是哪个版本的 zlib。

关于java - 使用 Java.util.zip.Inflater 解压缩字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9770364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com