简体   繁体   English

如何在Python中膨胀相同的数据

[英]How to Inflate the same data in Python

I have a code in Java which works fine, and I need to inflate the same data in python 我有一个工作正常的Java代码,我需要在python中填充相同的数据

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

    public static byte[] Inflate(byte[] compressedContent) throws IOException {
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(compressedContent), new Inflater(true));
        byte[] buffer = new byte[4096];

        int len;
        while ((len = iis.read(buffer)) != -1) {
            s.write(buffer, 0, len);
        }

        iis.close();
        s.flush();
        s.close();
        return s.toByteArray();
    }

Using 使用

StringUtils.newStringUtf8(inflate(Base64.decodeBase64("PZLHrptQAET_xevHE8VgnB1gyqVjig0bRLkUg-k9yr_HiZTsZo5mZjU_T1GSwHEMp7aCzenH6fR1-ivDae_gx7MwGuDwoWX6PwN3uYjFpDRK2XZRfnJQQXA5MIK3N_s7oEDFb9qruFmVNtmCtuuOX6qcTEVP5k-Hv7t-mVnfo-XgDa4LBkIt9lMmtKBz4kful_eDNBUONYQ95CXHBRY3dSlEYcC063oXC8hMkKLXRof6Re3vS8U1w-A0oRQt0spqnGifob-1orDhK-bMYflYVOR8KQC_YxVjjekaHuUxvQOZXBgdI4ubvl6z-p0BF-AjY2qNca48qY6j80Wa6Wxjvl8c31AG5V6vto8FG3vZ2c1jvt28MuvIdyjTx1otQPLMC71iOHjqtpFihNLmQVhPdSzbuM8rJ_eocJ4z12DzvFDZGwyeC109TGV2xjsQ32kv5VGB2NH1XFiGVd8xkE9PRI1oDHFwRck_25y3KlxMWKmlDrw7Br75nrunSsrNJbZwzq5rTRivAuhmBZz12RRacuxyeSz5ZIcMqFk8Il8U7nYEsLHHqLRP92oEGfvQZgfqLuuNWf-qlXqc56TiLpdjlfvAU-LwGG599wrdKST41sHeiKCbCZckNLW-aT8V0_tC7FzPh1pZWO6uykgGHtpOp0J9KzxKlPdXvwy9FTV0geUAmjERfR_mgwDciiqlr0qahOlKSMrW524DzAY4Fv8-18x1_XWCW1d-aFh-CE2dUfTXbw")))

The Java code works well, but I cannot convert it to Python as follows.. Java代码运行良好,但是我不能如下将其转换为Python。

def Base64UrlDecode(data):
    """Decode base64, padding being optional.

    :param data: Base64 data as an ASCII byte string
    :returns: The decoded byte string.

    """
    if isinstance(data, unicode):
        data = data.encode('utf-8')
    missing_padding = len(data) % 4
    if missing_padding != 0:
        data += b'=' * (4 - missing_padding)
    return base64.decodestring(data)

url_decode = Base64UrlDecode(token) # The token is the same string as the above one.
# https://docs.python.org/2/library/zlib.html#zlib.compressobj
for i in range(-15, 32): # try all possible ones, but none works.
    try:
        decode = zlib.decompress(url_decode, i)
    except:
        pass

The true in Inflater(true) in Java means inflation of raw deflate data with no header or trailer. trueInflater(true)在Java中是指与没有首或尾原始DEFLATE数据的充气。 To get that same operation in Python, the second argument to zlib.decompress() must be -15 . 要在Python中获得相同的操作, zlib.decompress()的第二个参数必须为-15 So you don't need to try different values there. 因此,您无需在那里尝试其他值。

The next thing to check is your Base64 decoding. 接下来要检查的是Base64解码。 The result of that must be different in the two cases, so look to see where they are different to find your bug. 在两种情况下,这样做的结果必须不同,因此请查看它们的不同之处以查找错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM