Python アプリケーションを Android に移植していますが、ある時点で、このアプリケーションは Web サービスと通信し、圧縮データを送信する必要があります。
そのために、次の方法を使用します。
def stuff(self, data):
"Convert into UTF-8 and compress."
return zlib.compress(simplejson.dumps(data))
次の方法を使用して、Android でこの動作をエミュレートしようとしています。
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 応答は正しくありません。これは、Java での圧縮の結果が Python での圧縮の結果と同じではないためだと思います。
zlib.compress と deflate の両方で "a" を圧縮する小さなテストを実行しました。
Python、zlib.compress() -> x%9CSJT%02%00%01M%00%A6
Android、Deflater.deflate -> H%EF%BF%BDK%04%00%00b%00b
Android でデータを圧縮して、Python で zlib.compress() の同じ値を取得するにはどうすればよいですか?
ヘルプ、ガイダンス、またはポインタは大歓迎です!