2

我写了一些Javascript代码。使用 base64 压缩并放气

function base64 (str) {
    return new Buffer(str).toString("base64");
}

function deflate (str) {
    return RawDeflate.deflate(str);
}

function encode (str) {
    return base64(deflate(str));
}
var str = "hello, world";
console.log("Test Encode");
console.log(encode(str));

我将“你好,世界”转换为 2f8d48710d6e4229b032397b2492f0c2

我想在java中解压这个字符串(2f8d48710d6e4229b032397b2492f0c2)

我将 str 放在一个文件中,然后:

public static String decompress1951(final String theFilePath) {
    byte[] buffer = null;

    try {
        String ret = "";
        System.out.println("can come to ret");

        InputStream in = new InflaterInputStream(new Base64InputStream(new FileInputStream(theFilePath)), new Inflater(true));
        System.out.println("can come to in");
        while (in.available() != 0) {
            buffer = new byte[20480];
*****line 64 excep happen            int len = in.read(buffer, 0, 20480);
            if (len <=0) {
                break;
            }
            ret = ret + new String(buffer, 0, len);
        }
        in.close();
        return ret;

    } catch (IOException e) {
        System.out.println("Has IOException");
        System.out.println(e.getMessage());

        e.printStackTrace();
    }
    return "";
}

但我有一个例外:

java.util.zip.ZipException: invalid stored block lengths
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at com.cnzz.mobile.datacollector.DecompressDeflate.decompress1951(DecompressDeflate.java:64)
    at com.cnzz.mobile.datacollector.DecompressDeflate.main(DecompressDeflate.java:128)
4

1 回答 1

0

那里的java代码运行良好。正如评论中一样,您不知何故弄错了编码值。我使用 javascript 值得到的编码值是y0jNycnXUSjPL8pJAQA=

然后,当您将此值复制到 file 并调用decompress1951时,您实际上会hello, world按要求返回。不知道在 javascript 部分该说什么,因为您使用的代码似乎与分发网页上的示例很好地同步。我注意到有原版前叉,所以那里可能有些混乱?无论如何,如果您想看一下,我认为可以将这个jsfiddle视为一个工作版本。

于 2012-10-18T19:23:25.637 回答