trying to gzip uncompress a StringIO

B

bob

I'm using the code below to read the zipped, base64 encoded WMF file
saved in an XML file with "Save as XML" from MS Word. As the "At this
point" comment shows, I know that the base64 decoding is going fine,
but unzipping from the decodedVersion StringIO object isn't getting me
anything, because the len(fileContent) call is returning 0. Any
suggestions?

thanks,

Bob

###########################################

import sys, base64, gzip, StringIO

infile = sys.argv[1]
outfile = sys.argv[2]

# When unencoding base64 file, write to a string that acts like a file
decodedVersion = StringIO.StringIO()

base64.decode(open(infile, 'r'),decodedVersion)

# At this point, if we write out decodedVersion.getvalue() to a file,
# gzip -d makes a proper wmf file from it, so we know everything's OK
so far.

# following based on
# http://www.diveintopython.org/http_web_services/gzip_compression.html

fileObj = gzip.GzipFile(fileobj=decodedVersion);
fileContent = fileObj.read()
print len(fileContent)
 
G

Gabriel Genellina

I'm using the code below to read the zipped, base64 encoded WMF file
saved in an XML file with "Save as XML" from MS Word. As the "At this
point" comment shows, I know that the base64 decoding is going fine,
but unzipping from the decodedVersion StringIO object isn't getting me
anything, because the len(fileContent) call is returning 0. Any
suggestions?

Perhaps GzipFile reads from the current position till end (and sees
nothing).
Try rewinding the file:
 
J

John Machin

Perhaps GzipFile reads from the current position till end (and sees
nothing).
Try rewinding the file:

The error would have been screamingly obvious on an open-reel mag. tape
drive :)

I suggest that "perhaps" and "try" are a little too tentative :)
GzipFile (or anything else that reads a file) is entitled to assume that
the file(-like) object it is given has had its read head positioned by
the caller at wherever the caller wants it to read from. Callers are
entitled to assume that the reader will not arbitrarily rewind (or skip
backwards/forwards to tape mark!) before reading.

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top