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

Joh*_*ohn 3 java zlib

我正在尝试解码收到的字符串。它使用这里的 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

该代码基本上是来自 Java API 的复制/粘贴。我也尝试过使用新的 Inflater(true); 即现在的包裹

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

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

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

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

问候

约翰

Mar*_*ler 5

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