是zlib.com preSS Python和Deflater.deflate关于Java(Android版)兼容? [英] Are zlib.compress on Python and Deflater.deflate on Java (Android) compatible?

查看:599
本文介绍了是zlib.com preSS Python和Deflater.deflate关于Java(Android版)兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植Python应用程序到Android,并在某些时候,这个应用程序与Web服务通信,它发送COM pressed数据。

I am porting a Python application to Android and, at some point, this application has to communicate with a Web Service, sending it compressed data.

在为了做到这一点,它使用下一个方法:

In order to do that it uses the next method:

def stuff(self, data):
    "Convert into UTF-8 and compress."
    return zlib.compress(simplejson.dumps(data))

我使用的一个方法,试图仿效Android的这种行为:

I am using the next method to try to emulate this behavior in 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中的COM pression的结果是不一样的一个Python编写的。

But the HTTP response from the server is not correct and I guess it is because the result of the compression in Java is not the same as the one in Python.

我跑了一个小的测试COM pressing一都与zlib.com preSS和放气。

I ran a little test compressing "a" both with zlib.compress and deflate.

Python中,zlib.com preSS() - > x%的9CSJT%02%00%01M%00%A6

Python, zlib.compress() -> x%9CSJT%02%00%01M%00%A6

Android的,Deflater.deflate - > H%EF%BF%BDK%04%00%00B%00B

Android, Deflater.deflate -> H%EF%BF%BDK%04%00%00b%00b

我应该怎样融为一体preSS在Android上的数据在Python获得zlib.com $ P $(PSS)的值相同?

How should I compress the data in Android to obtain the same value of zlib.compress() in Python?

任何帮助,指导或指针是极大的AP preciated!

Any help, guidance or pointer is greatly appreciated!

推荐答案

虽然它们不完全一样的算法,似乎他们是完全兼容的(也就是说,如果你的COM preSS,例如,字符串使用Deflater.deflate可以正确uncom preSS它使用zlib的)。

Although they are not exactly the same algorithms, it seems that they are totally compatible (meaning that if you compress, for example, an String using Deflater.deflate you can correctly uncompress it using zlib).

是什么原因造成我的问题是,在信息中的所有表单变量必须%的逃脱,而Android应用程序并没有这样做。编码数据为Base64发送它,并修改服务器脱uncom $ P $使用之前它的Base64 code。使用zlib的pssing之前解决了这个问题。

What caused my problem was that all form variables in a POST need to be percent escaped, and the Android application was not doing that. Encoding the data to Base64 before sending it, and modifying the server to decode it using Base64 before uncompressing it using zlib solved the problem.

这篇关于是zlib.com preSS Python和Deflater.deflate关于Java(Android版)兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆