![](/img/trans.png)
[英]Are zlib.compress on Python and Deflater.deflate on Java (Android) compatible?
[英]how can I implement zlib.compress in java
我想将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_string不同
GZIPOutputStream
将产生一个*.gz
文件。 如果只需要zlib流,请改用DeflaterOutputStream
。
// ... the rest are the same ...
DeflaterOutputStream os = new DeflaterOutputStream(baos);
// ... the rest are the same ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.