473,890 Members | 5,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Directory structure inside a ZipFile object

Hello everyone,

I'm a newbie to Python, learning it and tried to write the script that
would read file, zip it, and put in the target directory. I used
ZipFile module (code below).

It works nice, except the zipfile created contains the directory path
of the source file -- which I do NOT want to recreate. E.g. when
I compress 'c:\\temp\\zz.t xt', the resulting zipfile contains not just
'zz.txt' file, but also the whole directory, i.e. first directory
is 'c:', inside of it the directory 'temp' is located, and only then
the 'zz.txt' file is stored.

Changing current directory via os.chdir and then using the
filename itself (zz.txt) doesn't work, i.e. the source directory
structure is still re-created within the zipfile.

Code follows. TIA for reply,
def zf(sfpath, targetdir):
if (sys.platform[:3] == 'win'):
tgfpath=sfpath[2:]
else:
tgfpath=sfpath
zfdir=os.path.d irname(os.path. abspath(targetd ir) + tgfpath)
zfname=zfdir + '\\' + os.path.basenam e(tgfpath) + '.zip'
if(not os.path.isdir(z fdir)):
os.makedirs(zfd ir)
archive=ZipFile (zfname, 'w', ZIP_DEFLATED)
print 'zfname ' + zfname
archive.write(s fpath)
archive.close()
zf('c:\\temp\\z z.txt','c:\\tmp \\test1\\test2' ) zfname c:\tmp\test1\te st2\temp\zz.txt .zip


--
"Cultural ecstasy may have billions of participants, but it hardly
has a single friend." -- Charles Freund
Jul 18 '05 #1
2 3456
Bulba! wrote:
tried to read file, zip it, and put in the target directory....

It works nice, except the zipfile created contains the directory path
of the source file -- which I do NOT want to recreate.

Look into the two-argument form of the write command:

import zipfile
archive = zipfile.ZipFile ('box.zip', 'w', zipfile.ZIP_DEF LATED)
archive.write(' source/somewhere/one.txt', 'name.txt')
archive.write(' two.txt', 'another.txt')
archive.close()

--Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #2
On Thu, 09 Dec 2004 07:42:57 -0800, Scott David Daniels
<Sc***********@ Acm.Org> wrote:
Look into the two-argument form of the write command:
Well, I should have guessed that, it works. Thanks!

import zipfile
archive = zipfile.ZipFile ('box.zip', 'w', zipfile.ZIP_DEF LATED)
archive.write(' source/somewhere/one.txt', 'name.txt')
archive.write(' two.txt', 'another.txt')
archive.close()

--Scott David Daniels
Sc***********@ Acm.Org


--
It's a man's life in a Python Programming Association.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1752
by: irit hasid | last post by:
Hi, I've been trying to use JDK's ZipFile.java in order to extract a zipped file. the files inside my zip file have japanese names, and it throws an exception. there's a bug at SUN's database about it, but no workaround has been suggested. does anyone know a workaround for unzipping a ZipFile from Java ? thanks, Irit.
6
15253
by: Tung Wai Yip | last post by:
Can I add empty directory using zipfile? When I try to add a directory it complains that it is not a file. tung
7
2146
by: Max M | last post by:
I guess that the best approach is calling a shell tool with something like os.popen(). But I cannot seem to find any free tools. Winzip has a command line option, but for registered users only. That is bothersome if I want to install the script on other machines. The same for pkwares zip. At least I cannot seem to find a free version of it. gzip seems able to do the trick, but I need to install Cygwin, that's
1
1605
by: ralobao | last post by:
I have this code: try: file = zipfile.ZipFile(nome_arquivo) Gauge.start() #inicia o Gauge for element in file.namelist(): try: newFile = open(diretorio + element,"wb") except: newFile = open(diretorio + element + '/',"w")
1
1546
by: iamlevis3 | last post by:
Does Python have any internal facility for creating recursive archives of a directory? I'd like to avoid reliance on extenal tools (winzip,tar,etc). Thanks!
2
6004
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could use the System.DirectoryServices.ActiveDirectory namespace. I don't even know if this is the right way to go as I can't seem to find anything in that namespace that would help me query active directory for names. I can't use an LDAP query...
3
4903
bvdet
by: bvdet | last post by:
Following is an example that may provide a solution to you: """ Function makeArchive is a wrapper for the Python class zipfile.ZipFile 'fileList' is a list of file names - full path each name 'archive' is the file name for the archive with a full path """ import zipfile, os def makeArchive(fileList, archive):
8
3954
by: =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B | last post by:
I made a C/S network program, the client receive the zip file from the server, and read the data into a variable. how could I process the zipfile directly without saving it into file. In the document of the zipfile module, I note that it mentions the file-like object? what does it mean? class ZipFile( file]]) Open a ZIP file, where file can be either a path to a file (a string) or a file-like object.
10
38635
by: Bertsche | last post by:
I'm relatively new to python, and am trying to zip a directory containing several levels of files and folders. I can use the walk function to name each file in my directory, and I can use zipfile to zip a flat number of files in one folder, but I am having heck of a time trying to zip the whole directory. I want to zip it all to a file called "help.zip", and I want it to retain the original file structure. Of my several tries, here is the...
0
9823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10827
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10463
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7170
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
5854
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
6049
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4681
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

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.