使用Java.util.zip.Inflater解压缩字符串 [英] Decompressing a string using Java.util.zip.Inflater

查看:865
本文介绍了使用Java.util.zip.Inflater解压缩字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to decode a string that I receive. It's compressed using the deflater here: https://github.com/dankogai/js-deflate And then base64 encoded.

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

However when I use the java inflater I get the following error message: unknown compression method.

    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(真实);即nowrap

This code is basically a copy/paste from the Java API. I have also tried to use the new Inflater(true); I.e the nowrap

注意:当使用'nowrap'选项时,还需要提供额外的虚拟字节作为输入。这是ZLIB原生的要求库,以支持某些优化。

"Note: When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input. This is required by the ZLIB native library in order to support certain optimizations."

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

So where is this dummy byte supposed to be added? At the beginning or end of the "byte[] decodedByteArray" ?

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

So any ideas how to solve this problem? Do I only have to add the dummy byte, do I need to use some other methodes etc?

嗯,我想这就是所有的帮助!

Well, that's it I guess, all help is appreciated!

问候

John

推荐答案

84VWAVDY(f3 85 56 01 50 d8)的base64解码不是有效的原始deflate流,也不是有效的包装(zlib和gzip)deflate流。所以无论如何,试图夸大数据都不会令人满意。

The base64 decode of "84VWAVDY" (f3 85 56 01 50 d8) is not a valid raw deflate stream, nor a valid wrapped (zlib nor gzip) deflate stream. So no matter what, you will get no satisfaction trying to inflate that data.

这篇关于使用Java.util.zip.Inflater解压缩字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆