473,839 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2635
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.normpat h
split = os.path.split
for each in zipFile.namelis t():
if not each.endswith('/'):
root, name = split(each)
directory = norm(join(path, root))
if not isdir(directory ):
os.makedirs(dir ectory)
file(join(direc tory, name),
'wb').write(zip File.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(archnam e, 'r')
for path in arch.namelist() :
print path
dname = dirname(path)
if not exists(dname): os.makedirs(dna me)
if splitext(path)[1]:
f = open(path, 'wb')
f.write(arch.re ad(path))
f.close()
arch.close()

def pack(archname, paths):
arch = ZipFile(archnam e, '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(fnam e)
arch.close()

unpack('test.ja r')
pack('test2.jar ', ['com', 'META-INF'])
</code>

--
HTH,
Rob
May 9 '07 #3
On May 9, 5:10 pm, Rob Wolfe <r...@smsnet.pl wrote:
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(archnam e, 'r')
for path in arch.namelist() :
print path
dname = dirname(path)
if not exists(dname): os.makedirs(dna me)
if splitext(path)[1]:
f = open(path, 'wb')
f.write(arch.re ad(path))
f.close()
arch.close()

def pack(archname, paths):
arch = ZipFile(archnam e, '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(fnam e)
arch.close()

unpack('test.ja r')
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
3754
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
1860
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 return true but its invalid parameter when I try to set the image to the picture box.... Private Overloads Function Unzip(ByVal strSource As String, ByVal strFileToExtract As String, ByRef newms As MemoryStream) As Boolean If...
4
6031
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 subdirectory, move the zip file into the subdirectory, but unzips the contents into the root (current) directory. I want the contents of the zip file unloaded into the newly created subdirectory where the zip file is. Any ideas? Thanks.
4
7445
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 bytes. It makes me think that is some kinda environment/system issue but am not sure what it is. I have checked the permissions on the folder/file and it seems to be good. Any help is really appreciated.
0
1686
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 up if strerr: print >sys.stderr, strerr outlog.error(strerr)
5
7044
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 when I try to extract the files. Can any help me where is a sample to unzip a tar file TIA Julio
3
2165
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 = zipfile.ZipFile(zip1, 'r')
1
8542
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? <?php echo exec('unzip file.zip'); ?> i checked apache logs and it says It works perfectly fine when i unzip using SSH command line.. how to fix this problem.
2
6400
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
9856
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10910
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10654
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7021
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5683
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.