如何实施zlib.压缩在j中

2024-09-30 04:38:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把python代码重写成java。这是python代码:

        zipped = zlib.compress(data_json_upload.encode("utf-8"))
        print ("data encode")
        print (zipped)
        base64_bytes = base64.b64encode(zipped)
        base64_string = base64_bytes.decode('utf-8')

这是我的java代码: *你知道吗

        byte[] bytes = inputString.getBytes("UTF-8");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream os = new GZIPOutputStream(baos);
        os.write(bytes, 0, bytes.length);
        os.close();
        byte[] result = baos.toByteArray();
        System.out.println(result);
        byte [] base64=Base64.encodeBase64(result);
        String send= new String(base64,"UTF-8");

你知道吗* 但是,它们似乎对同一个字符串返回不同的结果。你知道我能做些什么来得到同样的代码在java上工作吗? 例如,在java中发送与在java中发送base64\u字符串不同的内容


Tags: 代码newdatabytesosjavaresultbyte
1条回答
网友
1楼 · 发布于 2024-09-30 04:38:26

GZIPOutputStream将生成一个*.gz文件。如果您只需要一个zlib流,请使用^{}。你知道吗

    // ... the rest are the same ...
    DeflaterOutputStream os = new DeflaterOutputStream(baos);
    // ... the rest are the same ...

相关问题 更多 >

    热门问题