나는 Python 애플리케이션을 Android로 포팅하고 있으며,이 애플리케이션은 웹 서비스와 통신하여 압축 된 데이터를 전송해야한다. 나는 안드로이드에서이 동작을 모방하려고 다음 방법을 사용하고Python의 zlib.compress와 Java (Android)의 Deflater.deflate는 호환됩니까?
def stuff(self, data):
"Convert into UTF-8 and compress."
return zlib.compress(simplejson.dumps(data))
:
private String compressString(String stringToCompress)
{
Log.i(TAG, "Compressing String " + stringToCompress);
byte[] input = stringToCompress.getBytes();
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
//compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try {
bos.close();
} catch (IOException e)
{
}
// Get the compressed data
byte[] compressedData = bos.toByteArray();
Log.i(TAG, "Finished to compress string " + stringToCompress);
return new String(compressedData);
}
을하지만에서 HTTP 응답은 다음 방법을 사용하여 그렇게하기 위해
서버가 올바르지 않습니다. 자바 압축 결과가 파이썬과 동일하지 않기 때문에 서버가 올바르지 않습니다.
나는 zlib.compress와 deflate를 사용하여 "a"를 약간 압축하여 테스트했습니다.
파이썬 zlib.compress() -> X % 9CSJT % 02퍼센트 00% 01M %의 00퍼센트의 A6
안드로이드 Deflater.deflate -> H의 %의 EF 체지방률의 %의 BDK % 04% 00% 송신 00b % 00b
Python에서 zlib.compress()와 동일한 값을 얻으려면 Android에서 데이터를 어떻게 압축해야합니까?
도움, 안내 또는 포인터가 대단히 감사합니다!
줄'return new String (compressedData);는 버그입니다. 그런 식으로 문자열을 사용할 수 없습니다. –