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



我正在尝试对接收到的字符串进行解码。使用此处的放气器进行压缩:https://github.com/dankogai/js-deflate然后进行base64编码。

然而,当我使用java充气程序时,我会收到以下错误消息:未知的压缩方法。

    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复制/粘贴的。我也尝试过使用新的充气机(真的);I.e现在的

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

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

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

好吧,我想就是这样,所有的帮助都很感激!

问候

John

将在末尾添加伪字节。但是,它只适用于zlib 1.1.4及更早版本。zlib的当前版本不需要它。我不确定java.util.zip使用的是什么版本的zlib。

"84VWAVDY"(f3 85 56 01 50 d8)的base64解码不是有效的原始放气流,也不是有效的包装(zlib或gzip)放气流。所以不管怎样,你都不会满足于夸大数据。

最新更新