[英]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.