distinction between unzipping bytes and unzipping a file

MRAB google at mrabarnett.plus.com
Fri Jan 9 15:16:01 EST 2009


webcomm wrote:
> Hi,
> In python, is there a distinction between unzipping bytes and
> unzipping a binary file to which those bytes have been written?
> 
Python's zipfile module can only read and write zip files; it can't 
compress or decompress data as a bytestring.

> The following code is, I think, an example of writing bytes to a file
> and then unzipping...
> 
> decoded = base64.b64decode(datum)
> #datum is a base64 encoded string of data downloaded from a web
> service
> f = open('data.zip', 'wb')
> f.write(decoded)
> f.close()
> x = zipfile.ZipFile('data.zip', 'r')
> 
> After looking at the preceding code, the provider of the web service
> gave me this advice...
> "Instead of trying to create a file, take the unzipped bytes and get a
> Unicode string of text from it."
> 
> If so, I'm not sure how to do what he's suggesting, or if it's really
> different from what I've done.
> 
If what you've been given is data which has been zipped and then base-64 
encoded, then I can't see that you might be doing wrong.

> I find that I am able to unzip the resulting data.zip using the unix
> unzip command, but the file inside contains some FFFD characters, as
> described in this thread...
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/4f57abea978cc0bf?hl=en#
> I don't know if the unwanted characters might be the result of my
> trying to write and unzip a file, rather than unzipping the bytes.
> The file does contain a semblance of what I ultimately want -- it's
> not all garbage.
> 
> Apologies if it's not appropriate to start a new thread for this.  It
> just seems like a different topic than how to deal with the resulting
> FFFD characters.
> 



More information about the Python-list mailing list