Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > difficult zipefile Question

Reply
Thread Tools

difficult zipefile Question

 
 
matthiasjanes
Guest
Posts: n/a
 
      10-06-2004
hello all,

i want to zip a folder plus the files and subfolders.

which works something like that:

#sample code

import os, zipfile

def zipit(path, ziper):

for root, dirs, files in os.walk(path):

for anyFile in files:

fullname = os.path.join(root, anyFile)

if __name__ == '__main__':

ziper = zipfile.ZipFile('test.zip', 'w')
zipit('somefolderTozip', ziper)
ziper.close()
#____________________________


this works fine as long as there is no empty subfolder.

can anyone tell me - or better give me a small example of code how to
add an empty folder to the zip archive.

thanks mr. janes
 
Reply With Quote
 
 
 
 
Dan Perl
Guest
Posts: n/a
 
      10-06-2004
I may be wrong, and I may have to eat my words later, but I think that the
zip file format does not handle empty folders. I tried it myself with
winzip just a few days ago. I think that zip only keeps a list of files
with their path so the folder will not be stored in the zip file if there is
no file in that folder. Like I already said, I may be wrong, but I would
recommend that you use tarfile instead.

Dan

"matthiasjanes" <> wrote in message
news: om...
> hello all,
>
> i want to zip a folder plus the files and subfolders.
>
> which works something like that:
>
> #sample code
>
> import os, zipfile
>
> def zipit(path, ziper):
>
> for root, dirs, files in os.walk(path):
>
> for anyFile in files:
>
> fullname = os.path.join(root, anyFile)
>
> if __name__ == '__main__':
>
> ziper = zipfile.ZipFile('test.zip', 'w')
> zipit('somefolderTozip', ziper)
> ziper.close()
> #____________________________
>
>
> this works fine as long as there is no empty subfolder.
>
> can anyone tell me - or better give me a small example of code how to
> add an empty folder to the zip archive.
>
> thanks mr. janes



 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      10-06-2004
matthiasjanes wrote:
> def zipit(path, ziper):
>
> for root, dirs, files in os.walk(path):
>
> for anyFile in files:
>
> fullname = os.path.join(root, anyFile)


Where is the "ziper" variable used? I don't see how this code does
anything except create an empty zipfile.

$ python samplecode.py

$ unzip test.zip
Archive: test.zip
warning [test.zip]: zipfile is empty

If you Google for <zipfile empty> you will find a python-list thread
with this solution in it:

http://mail.python.org/pipermail/pyt...ne/170526.html

Alternatively you could submit a patch or a bug.

HTH,
--
Michael Hoffman
 
Reply With Quote
 
Dan Perl
Guest
Posts: n/a
 
      10-06-2004
I'm going to take a break and have dinner. My words are what's on the menu.

Dan

"Michael Hoffman" < > wrote in
message news:ck1tfn$q64$...
> matthiasjanes wrote:
>> def zipit(path, ziper):
>>
>> for root, dirs, files in os.walk(path):
>>
>> for anyFile in files:
>>
>> fullname = os.path.join(root, anyFile)

>
> Where is the "ziper" variable used? I don't see how this code does
> anything except create an empty zipfile.
>
> $ python samplecode.py
>
> $ unzip test.zip
> Archive: test.zip
> warning [test.zip]: zipfile is empty
>
> If you Google for <zipfile empty> you will find a python-list thread with
> this solution in it:
>
> http://mail.python.org/pipermail/pyt...ne/170526.html
>
> Alternatively you could submit a patch or a bug.
>
> HTH,
> --
> Michael Hoffman



 
Reply With Quote
 
matthiasjanes
Guest
Posts: n/a
 
      10-07-2004
Michael Hoffman < > wrote in message news:<ck1tfn$q64$>...
> matthiasjanes wrote:
> > def zipit(path, ziper):
> >
> > for root, dirs, files in os.walk(path):
> >
> > for anyFile in files:
> >
> > fullname = os.path.join(root, anyFile)

>
> Where is the "ziper" variable used? I don't see how this code does
> anything except create an empty zipfile.
>



Sorry I did not post all the code:

#sample code

import os, zipfile, string

def zipit(path, ziper):
for root, dirs, files in os.walk(path):

for anyFile in files:

fullname = os.path.join(root, anyFile)
archiveName = string.lstrip( fullname,path + "\\" )

ziper.write(fullname, archiveName, zipfile.ZIP_DEFLATED )


if __name__ == '__main__':

ziper = zipfile.ZipFile('test.zip', 'w')
zipit('somefolderTozip', ziper)
ziper.close()
#______________________

all seems to work - just empty subfolders are gone.

I checkt it out - 7-zip for instance keps also empty subfolders.

maybe someone has an idea.

thanks MrJanes
 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      10-07-2004
matthiasjanes wrote:

> maybe someone has an idea.


Someone has more than an idea. I already posted the answer to your
question, but you apparently only bothered to read part of my message.
--
Michael Hoffman
 
Reply With Quote
 
matthiasjanes
Guest
Posts: n/a
 
      10-08-2004
Michael Hoffman < > wrote in message news:<ck4emv$9rb$>...
> matthiasjanes wrote:
>
> > maybe someone has an idea.

>
> Someone has more than an idea. I already posted the answer to your
> question, but you apparently only bothered to read part of my message.


Sorry M.Hoffman - I'm in the wrong. Somehow it confused me so much
that I had forgotten to post the whole code - so I did it straight
away and forgot to follow your instruction. once again Sorry and
Thanks for your help.

Mr.Janes
 
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
self extracting zipefile (windows) and (standard module) zipefile Werner Python 6 08-30-2007 06:42 AM
quick (but maybe difficult) question kingrundzap@hotmail.com DVD Video 1 09-02-2006 08:08 PM
difficult question?:) zbiszko XML 3 09-13-2005 09:04 AM
Really difficult OSPF question to routing gurus Oleg Tipisov Cisco 6 08-21-2004 08:30 PM
very difficult ISAPI filter question - help manunam C++ 0 10-23-2003 04:18 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