Unzip then Zip help

sdoty044 at gmail.com sdoty044 at gmail.com
Thu May 10 15:33:13 EDT 2007


On May 9, 5:10 pm, Rob Wolfe <r... at smsnet.pl> wrote:
> sdoty... at gmail.com writes:
> > 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:
> > ***unzipa 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
>
> Try this:
>
> <code>
> import os
> from os.path import dirname, exists, splitext, join
> from zipfile import ZipFile, ZIP_DEFLATED
>
> def unpack(archname):
>     arch = ZipFile(archname, 'r')
>     for path in arch.namelist():
>         print path
>         dname = dirname(path)
>         if not exists(dname): os.makedirs(dname)
>         if splitext(path)[1]:
>             f = open(path, 'wb')
>             f.write(arch.read(path))
>             f.close()
>     arch.close()
>
> def pack(archname, paths):
>     arch = ZipFile(archname, 'w', ZIP_DEFLATED)
>     for path in paths:
>         for root, dirs, files in os.walk(path):
>             for fname in files:
>                 fname = join(root, fname)
>                 print fname
>                 arch.write(fname)
>     arch.close()
>
> unpack('test.jar')
> pack('test2.jar', ['com', 'META-INF'])
> </code>
>
> --
> HTH,
> Rob- Hide quoted text -
>
> - Show quoted text -


Awsome, Thanks !!

Ran into one or 2 issues with the unpack and pack methods (related to
the JAR files I was using), but it was nothing I could not work out on
my own!!




More information about the Python-list mailing list