By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

trying to gzip uncompress a StringIO

P: n/a
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_w...mpression.html

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

May 22 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
En Tue, 22 May 2007 14:22:25 -0300, <bo*@snee.comescribió:
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:
decodedVersion = StringIO.StringIO()
base64.decode(open(infile, 'r'),decodedVersion)
decodedVersion.seek(0)
fileObj = gzip.GzipFile(fileobj=decodedVersion);
fileContent = fileObj.read()
print len(fileContent)
--
Gabriel Genellina

May 22 '07 #2

P: n/a
On 23/05/2007 7:48 AM, Gabriel Genellina wrote:
En Tue, 22 May 2007 14:22:25 -0300, <bo*@snee.comescribió:
>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:
>decodedVersion = StringIO.StringIO()
base64.decode(open(infile, 'r'),decodedVersion)
decodedVersion.seek(0)
>fileObj = gzip.GzipFile(fileobj=decodedVersion);
fileContent = fileObj.read()
print len(fileContent)
>>import StringIO
s = StringIO.StringIO()
s.write('blahblah\n')
s.read()
''
>>s.seek(0)
s.read()
'blahblah\n'
>>>
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
May 22 '07 #3

P: n/a
On May 23, 2:37 am, b...@snee.com wrote:
Perfect, thanks! Now I have a working WMF file and everything.

Bob
Now you just got to get your reply button working :-D

May 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.