Unzip then Zip help

S

sdoty044

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
 
K

kyosohma

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
 
R

Rob Wolfe

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>
 
S

sdoty044

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!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top