python error in decoding base64 string
<p>I'm trying to unzip a base64 string,</p>

<p>this is the code I'm using</p>

<pre><code>def unzip_string(s) :
    s1 = base64.decodestring(urllib.unquote(s))
    sio = StringIO.StringIO(s1)
    gzf = gzip.GzipFile(fileobj=sio)
    guff = gzf.read()
    return json.loads(guff)
</code></pre>

<p>i'm getting error Error: Incorrect padding</p>

<p>where I try to unzip the same string using node.js code it works without a problem.</p>

<p>where:</p>

<pre><code>s == H4sIAAAAAAAAA22PW0/CQBCF/8s81wQosdA3TESJhhhb9cHwMN1O6Ybtbt0LhDT97+5yU4yPc+bMnO90YCyyDaSfHRimieQSG4IUaldABC1qbAykHbQsrzWZWokSUumEiMCQ3nJGCy9ADH0EFvWarJ+eHv11v4qgEIptqHyTlovzWes0q9HQ3X87Lh80Msp5gDhqzGlN0or9B1pWU5ldxV72c2/ODg0C7lUXu/U2p8XLpY35+6Mmtsn4WqLILFrnTRUKQxFwk7+fSL23+zX215VD/jE16CeojIzhSi5kpQ6xzVkIz76wuSmHRVINRuVtheMxDuLJJB5Nk5hRMkriaTGJh8MDn5LWv8v3bejzvFjez15/5EsNbuZo7FzpHepyJoTaBWqrHfX9N0/UAJ7qAQAA.bi0I1YDZ3V6AXu6aYTGO1JWi5tE5CoZli7aa6bFtqM4
</code></pre>

<p>I've seen some suggestions to add '=' and other magic but it just results in the gzip module failing to open the file.</p>

<p>any ideas?</p>

<p>This worked for me (Python 3). The padding is indeed important, as you've seen in other answers:</p>

<pre><code>import base64
import zlib
import json

s = b'H4sIAAAAAAAAA22PW0/CQBCF/8s81wQosdA3TESJhhhb9cHwMN1O6Ybtbt0LhDT97+5yU4yPc+bMnO90YCyyDaSfHRimieQSG4IUaldABC1qbAykHbQsrzWZWokSUumEiMCQ3nJGCy9ADH0EFvWarJ+eHv11v4qgEIptqHyTlovzWes0q9HQ3X87Lh80Msp5gDhqzGlN0or9B1pWU5ldxV72c2/ODg0C7lUXu/U2p8XLpY35+6Mmtsn4WqLILFrnTRUKQxFwk7+fSL23+zX215VD/jE16CeojIzhSi5kpQ6xzVkIz76wuSmHRVINRuVtheMxDuLJJB5Nk5hRMkriaTGJh8MDn5LWv8v3bejzvFjez15/5EsNbuZo7FzpHepyJoTaBWqrHfX9N0/UAJ7qAQAA.bi0I1YDZ3V6AXu6aYTGO1JWi5tE5CoZli7aa6bFtqM4'

decoded = base64.urlsafe_b64decode(s + b'=')
uncompressed = zlib.decompress(decoded, 16 + zlib.MAX_WBITS)
unjsoned = json.loads(uncompressed.decode('utf-8'))

print(unjsoned)
</code></pre>

<p>The <code>zlib.decompress(decoded, 16 + zlib.MAX_WBITS)</code> is a slightly more compact way to un-gzip a byte string.</p>

python base64 gzip