Unzip then Zip help
Rob Wolfe
rw at smsnet.pl
Wed May 9 17:10:12 EDT 2007
sdoty044 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:
> ***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
>
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
More information about the Python-list
mailing list