Unzip then Zip help

kyosohma at gmail.com kyosohma at gmail.com
Wed May 9 16:55:45 EDT 2007


On May 9, 2:36 pm, sdoty... at gmail.com wrote:
> I am a true n00b... and I just using Python to complete some very
> small uneventful task, but need help with one last thing.
>
> Basically, this I what I am trying to do.
>
> make a temp directory (this part I can do)
>
> Need help with:
> ***unzip a JAR file with the complete list of subdirectories w/
> files****
>
> modify the a set of XML files (this part I can do)
>
> Need help with:
> ***then zip the entire contents of the temp directory with sub
> directories***
>
> The only thing I am having trouble with is the whole directory stuff,
> if this was just straight files, no problem.
>
> Any help would be appreciated

I would use the subprocess module and command line flags for whatever
zip client software you use to get the job done. For example, I have
Filzip and IZArc, both of which support command line unzipping and
zipping.

One of my co-workers came up with a way to unzip a zipped file:

def unzip(path, zipFile):
    """ Unzips file specified in above dictionary """
    isdir = os.path.isdir
    join = os.path.join
    norm = os.path.normpath
    split = os.path.split
    for each in zipFile.namelist():
        if not each.endswith('/'):
            root, name = split(each)
            directory = norm(join(path, root))
            if not isdir(directory):
                os.makedirs(directory)
            file(join(directory, name),
'wb').write(zipFile.read(each))

# where path is the location you want to extract to and "zipFile" is
the file.zip

Good luck!

Mike




More information about the Python-list mailing list