469,299 Members | 2,088 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

trying to gzip uncompress a StringIO

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
3 5909
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
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
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.

Similar topics

reply views Thread by Greg Bakken | last post: by
3 posts views Thread by rohisingh | last post: by
7 posts views Thread by ckb2102 | last post: by
10 posts views Thread by Xah Lee | last post: by
21 posts views Thread by Paul Rubin | last post: by
2 posts views Thread by Jonathan Bowlas | last post: by
reply views Thread by erikcw | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.