![](/img/trans.png)
[英]How to decompress String in Java which is compressed in VBA using zlib
[英]Java: How to restore string data which was compressed with python's zlib encoder
我通過套接字從python發送數據到java。
所以在python 2.7方面,我有:
s = "this is test str"
compressed = s.encode('zlib')
push_to_tcp_socket(compressed)
所以我需要在java端恢復初始字符串。 我怎么能這樣做?
您需要發送字符串的長度,或者關閉連接,以便知道最后一個字節的位置。
最有可能幫助您的類是DeflatorInputStream,您可以在讀取字節后使用它。 這是zlib類的裸包裝器。 我沒有測試它與python一起工作,但它是你最好的選擇。
您可以嘗試其他壓縮,如Snappy或LZ4,它們具有跨平台支持。
我假設您已經了解Java上的網絡部分。 您可以使用Inflater
類來獲取javadocs中的字符串
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
//Then create string in java i assumed you are using python 2 and string is ASCII
String str = new String(result,"US-ASCII")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.