Recipe2.10.Handling a zip File Inside a String


Recipe 2.10. Handling a zip File Inside a String

Credit: Indyana Jones

Problem

Your program receives a zip file as a string of bytes in memory, and you need to read the information in this zip file.

Solution

Solving this kind of problem is exactly what standard library module cStringIO is for:

import cStringIO, zipfile class ZipString(ZipFile):     def _ _init_ _(self, datastring):         ZipFile._ _init_ _(self, cStringIO.StringIO(datastring))

Discussion

I often find myself faced with this taskfor example, zip files coming from BLOB fields in a database or ones received from a network connection. I used to save such binary data to a temporary file, then open the file with the standard library module zipfile. Of course, I had to ensure I deleted the temporary file when I was done. Then I thought of using the standard library module cStringIO for the purpose . . . and never looked back.

Module cStringIO lets you wrap a string of bytes so it can be accessed as a file object. You can also do things the other way around, writing into a cStringIO.StringIO instance as if it were a file object, and eventually recovering its contents as a string of bytes. Most Python modules that take file objects don't check whether you're passing an actual filerather, any file-like object will do; the module's code just calls on the object whatever file methods it needs. As long as the object supplies those methods and responds correctly when they're called, everything just works. This demonstrates the awesome power of signature-based polymorphism and hopefully teaches why you should almost never type-test (utter such horrors as if type(x) is y, or even just the lesser horror if isinstance(x, y)) in your own code! A few low-level modules, such as marshal, are unfortunately adamant about using "true" files, but zipfile isn't, and this recipe shows how simple it makes your life!

If you are using a version of Python that is different from the mainstream C-coded one, known as "CPython", you may not find module cStringIO in the standard library. The leading c in the name of the module indicates that it's a C-specific module, optimized for speed but not guaranteed to be in the standard library for other compliant Python implementations. Several such alternative implementations include both production-quality ones (such as Jython, which is coded in Java and runs on a JVM) and experimental ones (such as pypy, which is coded in Python and generates machine code, and IronPython, which is coded in C# and runs on Microsoft's .NET CLR). Not to worry: the Python Standard Library always includes module StringIO, which is coded in pure Python (and thus is usable from any compliant implementation of Python), and implements the same functionality as module cStringIO (albeit not quite as fast, at least on the mainstream CPython implementation). You just need to alter your import statement a bit to make sure you get cStringIO when available and StringIO otherwise. For example, this recipe might become:

import zipfile try:     from cStringIO import StringIO except ImportError:     from StringIO import StringIO class ZipString(ZipFile):     def _ _init_ _(self, datastring):         ZipFile._ _init_ _(self, StringIO(datastring))

With this modification, the recipe becomes useful in Jython, and other, alternative implementations.

See Also

Modules zipfile and cStringIO in the Library Reference and Python in a Nutshell; Jython is at http://www.jython.org/; pypy is at http://codespeak.net/pypy/; IronPython is at http://ironpython.com/.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net