java - "java.util.zip.DataFormatException: incorrect header check"在 JavaScript 中放气然后在 Java 中膨胀

标签 java javascript base64 deflate inflate

我在页面中有一个大数据,需要发布到服务器。

所以我使用 https://github.com/dankogai/js-deflate 来压缩大数据。

看起来工作正常。

helloworld -> w4tIw43DicOJL8OPL8OKSQEA

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<h1>Demo</h1>
<p>$Id: demo.html,v 0.4 2013/04/09 14:25:38 dankogai Exp dankogai $</p>
<dl>
<dt>Inflated + Base64-Decoded (Original):</dt>
<dd><textarea id="inflated" cols="64" rows="16" onkeyup="(function(that, dst){
  setTimeout(function(){
    dst.value = Base64.toBase64(RawDeflate.deflate(Base64.utob(that.value)));
  },0)
})(this,$('deflated'))"></textarea></dd>
<dt>Deflated + Base64-Encoded (Compressed):</dt>
<dd><textarea id="deflated" cols="64" rows="16" onkeyup="(function(that, dst){
  setTimeout(function(){
    dst.value = Base64.btou(RawDeflate.inflate(Base64.fromBase64(that.value)));
  },0);
})(this, $('inflated'))"></textarea></dd>
</dl>
<script src="./base64.js"></script>
<script src="../rawinflate.js"></script>
<script src="../rawdeflate.js"></script>
<script>
$ = function(id){ return document.getElementById(id) };
</script>
</body>
</html>

enter image description here

所以我想通过将 deflate 的结果放入 java 代码中来获得 hellowold 我得到了错误:

java.util.zip.DataFormatException: incorrect header check

enter image description here

以下是我的代码:

public static void main(String[] args) throws IOException {

        try {
            // Encode a String into bytes
            String output1 = "w4tIw43DicOJL8OPL8OKSQEA";

            byte[] output2 = Base64Util.decode(output1);

            // Decompress the bytes
            Inflater decompresser = new Inflater();
            decompresser.setInput(output2);

            System.out.println("a:" + new String(output2));

            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, "UTF-8");
            System.out.println("b:" + outputString);

        } catch (java.io.UnsupportedEncodingException ex) {
            ex.printStackTrace();
            // handle
        } catch (java.util.zip.DataFormatException ex) {
            ex.printStackTrace();
            // handle
        }

    }

那么问题是什么?

这是我测试过的 Base64Util 类。没关系:

public class Base64Util {

    private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

    private static int[]  toInt   = new int[128];

    static {
        for(int i=0; i< ALPHABET.length; i++){
            toInt[ALPHABET[i]]= i;
        }
    }

    /**
     * Translates the specified byte array into Base64 string.
     *
     * @param buf the byte array (not null)
     * @return the translated Base64 string (not null)
     */
    public static String encode(byte[] buf){
        int size = buf.length;
        char[] ar = new char[((size + 2) / 3) * 4];
        int a = 0;
        int i=0;
        while(i < size){
            byte b0 = buf[i++];
            byte b1 = (i < size) ? buf[i++] : 0;
            byte b2 = (i < size) ? buf[i++] : 0;

            int mask = 0x3F;
            ar[a++] = ALPHABET[(b0 >> 2) & mask];
            ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
            ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
            ar[a++] = ALPHABET[b2 & mask];
        }
        switch(size % 3){
            case 1: ar[--a]  = '=';
            case 2: ar[--a]  = '=';
        }
        return new String(ar);
    }

    /**
     * Translates the specified Base64 string into a byte array.
     *
     * @param s the Base64 string (not null)
     * @return the byte array (not null)
     */
    public static byte[] decode(String s){
        int delta = s.endsWith( "==" ) ? 2 : s.endsWith( "=" ) ? 1 : 0;
        byte[] buffer = new byte[s.length()*3/4 - delta];
        int mask = 0xFF;
        int index = 0;
        for(int i=0; i< s.length(); i+=4){
            int c0 = toInt[s.charAt( i )];
            int c1 = toInt[s.charAt( i + 1)];
            buffer[index++]= (byte)(((c0 << 2) | (c1 >> 4)) & mask);
            if(index >= buffer.length){
                return buffer;
            }
            int c2 = toInt[s.charAt( i + 2)];
            buffer[index++]= (byte)(((c1 << 4) | (c2 >> 2)) & mask);
            if(index >= buffer.length){
                return buffer;
            }
            int c3 = toInt[s.charAt( i + 3 )];
            buffer[index++]= (byte)(((c2 << 6) | c3) & mask);
        }
        return buffer;
    } 

}

最佳答案

“w4tIw43DicOJL8OPL8OKSQEA”不是有效原始 deflate 流的 Base-64 编码。所以它对我来说似乎不太好用。我真的不知道你的 Javascript 代码在做什么。在您的问题的评论中查看我的问题。

即使是这样,您的 Java 代码也需要对 deflate 数据进行 zlib 包装,而编写 Javascript 代码是为了在没有包装的情况下生成和使用原始 deflate 数据。要让 Java 膨胀原始压缩数据,您需要:

Inflater decompresser = new Inflater(true);

选择 nowrap 选项。

关于java - "java.util.zip.DataFormatException: incorrect header check"在 JavaScript 中放气然后在 Java 中膨胀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862419/

相关文章:

Java图形绘制带特定参数的圆

actionscript-3 - Flash AS3和Base64库=编译错误

java - 如何将当前方法的参数放入列表中

java - 重用 ObjectInputStream 会抛出 StreamCorruptedException

javascript - Google map 3.0 - 有时删除标记后,它们仍然存在

javascript - div 中的 ng-repeat 不起作用

javascript - 在所有其他内容上导航以及与导航一起响应内容?

ruby-on-rails - 为什么 ruby​​ base64 与其他不同?

Java缩略图库: IIOException occurred : Error reading PNG metadata

java - 使用 java 的 keystore 格式无效