By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

Unzip then Zip help

P: n/a
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

May 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 9, 2:36 pm, sdoty...@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

May 9 '07 #2

P: n/a
sd******@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
May 9 '07 #3

P: n/a
On May 9, 5:10 pm, Rob Wolfe <r...@smsnet.plwrote:
sdoty...@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!!

May 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.