difficult zipefile Question

M

matthiasjanes

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
 
D

Dan Perl

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
 
M

Michael Hoffman

matthiasjanes said:
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/python-list/2003-June/170526.html

Alternatively you could submit a patch or a bug.

HTH,
 
D

Dan Perl

I'm going to take a break and have dinner. My words are what's on the menu.

Dan

Michael Hoffman said:
matthiasjanes said:
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/python-list/2003-June/170526.html

Alternatively you could submit a patch or a bug.

HTH,
 
M

matthiasjanes

Michael Hoffman said:
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
 
M

Michael Hoffman

matthiasjanes said:
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.
 
M

matthiasjanes

Michael Hoffman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top