473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Unzip then Zip help

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
3 2607
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jyoti.khera | last post by:
Hi, Can anyone tell me if there is any inbuilt utility in windows shell API with the help of which we can Unzip the files. I need to use that and Unzip the files using c# code. Regards Jyoti
1
by: Jean Christophe Avard | last post by:
Hi! Finally I figure out what was wrong... "objZipEntry.size = strmFile" I wasn't giving the right file length... But now, I have issue with the unzip function... I can't unzip it, the unzip function...
4
by: DataSmash | last post by:
I need to unzip all zip file(s) in the current directory into their own subdirectories. The zip file name(s) always start with the string "usa" and end with ".zip". The code below will make the...
4
by: spdude | last post by:
Hi, We are using Dynazip to unzip files on our server. It works perfectly on a test box but when the same code is deployed to the production, it chokes. It creates the unzipped file with 0...
0
by: Rocky Zhou | last post by:
python unzip At first, I tried to use 'os.popen3("unzip ...") like this: fin, fout, ferr = os.popen3("unzip -o -d %s %s" % (dest, zipfile)) strerr = ferr.read() # This makes the program hanging...
5
by: =?Utf-8?B?anVsaW8=?= | last post by:
Hi, I write a program to unzip a Tar file generated on a Unix environment file using SharpZipLib, but returns a error "Header checksum is invalid" when execute the program. This error appears...
3
by: jld730 | last post by:
Hi All, I am looking for help on this simple script to unzip/extract the contents of a zip file. This is what I have so far: import zipfile, os, sys zip1 = ("C:\\Temp\\test11.zip") z =...
1
by: olddocks | last post by:
I want to upload a zip file and then extract/unzip it. I am accomplishing this with php exec command. I am calling unzip from php exec command within a php script and it is not extracting files. why?...
2
by: somsub | last post by:
Hi all, Here is my samle code use strict ; use warnings ; use IO::Uncompress::Unzip ; When I compiled this three lines of code in win32 I got error like below.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.