javascript deflate 和 java.util.zip.Deflater 之间有什么区别

发布于 2024-12-31 22:05:26 字数 1710 浏览 0 评论 0原文

我写了一些 JavaScript 代码。 使用base64和deflate进行压缩

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

我将“hello,world”转换为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)

I wrote some Javascript code.
compress with base64 and deflate

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

I converted "hello, world" to 2f8d48710d6e4229b032397b2492f0c2

and I want to decompress this string(2f8d48710d6e4229b032397b2492f0c2) in java

I put the str in a file, then:

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

But I have an exception:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狂之美人 2025-01-07 22:05:26

上面的java代码运行得很好。正如评论中所述,您以某种方式得到了错误的编码值。我使用 javascript 值获得的编码值是 y0jNycnXUSjPL8pJAQA=

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

The java code up there works perfectly. As in the comment, you somehow got the encoded value wrong. The encoded value I got using the javascript value is y0jNycnXUSjPL8pJAQA=

Then, when you copy this value to file and call decompress1951, you do in fact get back hello, world as required. Don't know what to say on the javascript part as the code you use seems to sync up nicely with examples on the distribution web pages. I notice there is the original and the fork so maybe there is some confusion there? Anyhow there is this jsfiddle which I think can be seen as a working version if you want to take a look at that one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文