最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

"java.util.zip.DataFormatException: incorrect header check" when deflate in JavaScript then inflate in Java -

programmeradmin3浏览0评论

I have a big data in the page and need to post to the server.

so I use to deflate the big data.

It seems that working fine.

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>

so I want to get hellowold by putting the result of deflate into the java code I got the error:

java.util.zip.DataFormatException: incorrect header check

the following is my code:

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

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

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

            // Depress the bytes
            Inflater depresser = new Inflater();
            depresser.setInput(output2);

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

            byte[] result = new byte[100];
            int resultLength = depresser.inflate(result);
            depresser.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
        }

    }

so what's the problem?

And this is my Base64Util Class, I've tested. it's ok:

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;
    } 

}

I have a big data in the page and need to post to the server.

so I use https://github./dankogai/js-deflate to deflate the big data.

It seems that working fine.

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>

so I want to get hellowold by putting the result of deflate into the java code I got the error:

java.util.zip.DataFormatException: incorrect header check

the following is my code:

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

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

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

            // Depress the bytes
            Inflater depresser = new Inflater();
            depresser.setInput(output2);

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

            byte[] result = new byte[100];
            int resultLength = depresser.inflate(result);
            depresser.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
        }

    }

so what's the problem?

And this is my Base64Util Class, I've tested. it's ok:

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;
    } 

}
Share Improve this question edited Sep 16, 2014 at 7:03 chanjianyi asked Sep 16, 2014 at 6:56 chanjianyichanjianyi 6154 gold badges15 silver badges37 bronze badges 1
  • What is utob and btou? – Mark Adler Commented Sep 16, 2014 at 16:47
Add a ment  | 

2 Answers 2

Reset to default 7

"w4tIw43DicOJL8OPL8OKSQEA" is not a Base-64 encoding of a valid raw deflate stream. So it does not seem to be working fine to me. I can't really tell what your Javascript code is doing. See my questions in the ments to your question.

Even if it were, your Java code is expecting a zlib wrapper on the deflate data, whereas the Javascript code is written to produce and consume raw deflate data without a wrapper. To get Java to inflate raw deflate data, you would need to:

Inflater depresser = new Inflater(true);

to select the nowrap option.

Finally, I found the solution. Maybe it is not the best solution, but solve my problem.

although with not good performance.

use the Javascript both client side and server side(Java js engine)..

So I can keep the result are same..

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论