Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Directory structure inside a ZipFile object

Reply
Thread Tools

Directory structure inside a ZipFile object

 
 
Bulba!
Guest
Posts: n/a
 
      12-09-2004
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.txt', 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.dirname(os.path.abspath(targetdir) + tgfpath)
zfname=zfdir + '\\' + os.path.basename(tgfpath) + '.zip'
if(not os.path.isdir(zfdir)):
os.makedirs(zfdir)
archive=ZipFile(zfname, 'w', ZIP_DEFLATED)
print 'zfname ' + zfname
archive.write(sfpath)
archive.close()

>>> zf('c:\\temp\\zz.txt','c:\\tmp\\test1\\test2')

zfname c:\tmp\test1\test2\temp\zz.txt.zip
>>>




--
"Cultural ecstasy may have billions of participants, but it hardly
has a single friend." -- Charles Freund
 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      12-09-2004
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_DEFLATED)
archive.write('source/somewhere/one.txt', 'name.txt')
archive.write('two.txt', 'another.txt')
archive.close()

--Scott David Daniels

 
Reply With Quote
 
 
 
 
Bulba!
Guest
Posts: n/a
 
      12-09-2004
On Thu, 09 Dec 2004 07:42:57 -0800, Scott David Daniels
<> 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_DEFLATED)
> archive.write('source/somewhere/one.txt', 'name.txt')
> archive.write('two.txt', 'another.txt')
> archive.close()
>
>--Scott David Daniels
>




--
It's a man's life in a Python Programming Association.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Directory structure of home directory? john HTML 4 06-15-2006 06:50 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Problem while trying to extract a directory from a zipfile. ralobao Python 1 12-08-2004 05:04 PM
add empty directory using zipfile? Tung Wai Yip Python 6 06-26-2003 09:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57