Welcome, guest | Sign In | My Account | Store | Cart

given a string that has the contents of a .zip, how to handle ? (or "on using StringIO and zipfile")

Python, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import cStringIO;
from zipfile import *

class ZipString(ZipFile):
    def __init__(self, string=None):
        if string==None:
            raise RuntimeError, 'must pass a string to constructor';
        self.fp = cStringIO.StringIO( str(string) );
        self._filePassed = 0;
        self.debug = 0  # Level of printing: 0 through 3
        self.NameToInfo = {}    # Find file info given name
        self.filelist = []      # List of ZipInfo instances for archive
        #self.compression = compression  # Method of compression
        self.mode = key = 'r';
        self.filename = "<string>"
        modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
        self._GetContents();

why use? mainly for zip files inside blob fields, or zips received from an network conn, keeping the ideia of not doing an I/O operation to save, handle and delete the zip.

how to use: pass the string in the constructor then use the normal methods of zipfile.

future: as a python-rookie i welcome any improvements.