Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

uncompress base64-gzipped string

2,343 views
Skip to first unread message

Niels Egberts

unread,
Jun 12, 2009, 4:58:42 PM6/12/09
to pytho...@python.org
I'm creating a game in python and I want to a level editor called
'tiled'. It ouputs a .xml file with base64-gzipped information. Then
every 4 bytes represents a number. See the following link:

http://www.mapeditor.org/wiki/Examining_the_map_format

To start I tried:
code = base64.b64decode("H4sIAAAAAAAAAO3NoREAMAgEsLedAfafE4+s6l0jolNJiif18tt/Fj8AAMC9ARtYg28AEAAA")
code = zlib.decompress(code)

But I get:
zlib.error: Error -3 while decompressing data: incorrect header check

How can I solve this?

Thanks,
Niels.

John Machin

unread,
Jun 12, 2009, 10:38:05 PM6/12/09
to pytho...@python.org
Niels Egberts <niels.egberts <at> gmail.com> writes:

> zlib.error: Error -3 while decompressing data: incorrect header check
>
> How can I solve this?

The link you quoted says "you need to first base64 decode
the string, then gunzip the resulting data" ... so gunzip it:

| >>> s0 =
"H4sIAAAAA......"
| >>> import base64
| >>> s1 = base64.b64decode(s0)
| >>> len(s0)
| 72
| >>> len(s1)
| 54
| >>> import StringIO
# or cStringIO or io depending on what
# Python versions you want to support
| >>> sio = StringIO.StringIO(s1)
| >>> import gzip
| >>> gzf = gzip.GzipFile(fileobj=sio)
| >>> guff = gzf.read()
| >>> len(guff)
| 4096
| >>> guff[:100]
| '\x1b\x00\x00\x00\x1b\x00\x00\x00\x1b\x00\x00\x00 [snip]

What a long journey: parse xml, base64 decode, gunzip,
and you're still not home; next stop is struct.unpack ...

HTH,
John


Niels Egberts

unread,
Jun 13, 2009, 5:02:20 AM6/13/09
to John Machin, pytho...@python.org
On Sat, Jun 13, 2009 at 4:38 AM, John Machin<sjma...@lexicon.net> wrote:
> | >>> guff[:100]
> | '\x1b\x00\x00\x00\x1b\x00\x00\x00\x1b\x00\x00\x00 [snip]
>
> What a long journey: parse xml, base64 decode, gunzip,
> and you're still not home; next stop is struct.unpack ...
>
> HTH,
> John

Thanks for the help! I dont think I could have done that.

And thanks for the tip on struct.unpack I managed to get a list of
integers to work with.

Niels

Scott David Daniels

unread,
Jun 13, 2009, 12:29:02 PM6/13/09
to
Niels Egberts wrote:
> On Sat, Jun 13, 2009 at 4:38 AM, John Machin<sjma...@lexicon.net> wrote:
> ...

> And thanks for the tip on struct.unpack I managed to get a list of
> integers to work with.

You might make do with array.fromstring (perhaps using .byteswap
afterwards) to get your numbers if you are talking more than a handful.

--Scott David Daniels
Scott....@Acm.Org

Lawrence D'Oliveiro

unread,
Jun 14, 2009, 10:26:13 PM6/14/09
to
In message <mailman.1517.1244860...@python.org>, John
Machin wrote:

> What a long journey: parse xml, base64 decode, gunzip,
> and you're still not home; next stop is struct.unpack ...

Binary data in an XML file?? Were these the same folks who worked on OOXML,
by any chance?

0 new messages