add empty directory using zipfile?

T

Tung Wai Yip

Can I add empty directory using zipfile? When I try to add a directory
it complains that it is not a file.

tung
 
J

JanC

Tung Wai Yip said:
Can I add empty directory using zipfile? When I try to add a directory
it complains that it is not a file.

ZIP files can't contain directories, only files and the paths to those
files. A workaround might be to put an empty file in the directory.
 
B

Ben Finney

ZIP files can't contain directories, only files and the paths to those
files. A workaround might be to put an empty file in the directory.

A better solution might be to use tar (or cpio) for archiving, since
they can add directories as well as files, and can also preserve file
permissions from Unix filesystems.

You then get the benefit of choosing whatever compression method you
like for the resultant archive. gzip and bzip2 both offer superior
compression to the built-in compression for Zip.
 
O

Oren Tirosh

ZIP files can't contain directories, only files and the paths to those
files. A workaround might be to put an empty file in the directory.

ZIP files *can* contain directories. They are described as zero-length
files with some flag set. I don't see any specific API for this in
zipfile.py but I think that if you pass a ZipInfo record with the right
values to ZipFile.writestr it should probably work. Consult the
documentation of the zip format or just create a zip containing an empty
directory with any zip utility look at the ZipInfo using zipfile.py.

Oren
 
J

JanC

Ben Finney said:
A better solution might be to use tar (or cpio) for archiving, since
they can add directories as well as files, and can also preserve file
permissions from Unix filesystems.

You then get the benefit of choosing whatever compression method you
like for the resultant archive. gzip and bzip2 both offer superior
compression to the built-in compression for Zip.

You can use bzip2 compression in a zip file (it's in the recent specs), but
probably not with zipfile, and most zip utilities can't uncompress it
anyway.

A disadvantage of .tar.whatever is that it is stream-based: no random
access to a single file in the archive...
 
J

JanC

Oren Tirosh said:
ZIP files *can* contain directories. They are described as zero-length
files with some flag set. I don't see any specific API for this in
zipfile.py but I think that if you pass a ZipInfo record with the right
values to ZipFile.writestr it should probably work. Consult the
documentation of the zip format or just create a zip containing an empty
directory with any zip utility look at the ZipInfo using zipfile.py.

I checked with some Windows ZIP utilities and it seems like they don't add
empty directories--oh wait it's an option that's off by default...
( *oops* ;)

They are added as an empty "file", with a filename sometimes ending in "/"
and sometimes not (this is not defined in the ZIP format specs?), and the
"directory" external attribute set.


The following script works for me:
=========================================
import zipfile

zf = zipfile.ZipFile('zftest.zip', 'w')

# This works:
zfi = zipfile.ZipInfo('test1/')
zf.writestr(zfi, '')

# This works too:
zfi = zipfile.ZipInfo('test2')
zfi.external_attr = 16
zf.writestr(zfi, '')

# But this doesn't:
#
# zf.write('test3')
#
# And this doesn't either:
#
# zf.write('test4/')

zf.close()
=========================================

Seems like the zipfile module requires the trailing "/" and/or manual
setting the attribute for directories in the ZipInfo object. If you want
to preserve other attributes you'll have to set them manually...

And the write() method doesn't work with directories... :-(
In fact the write() method is broken and doesn't store any file attributes
correctly on Windows...
 
T

Tung Wai Yip

ZIP files *can* contain directories. They are described as zero-length
files with some flag set. I don't see any specific API for this in
zipfile.py but I think that if you pass a ZipInfo record with the right
values to ZipFile.writestr it should probably work. Consult the
documentation of the zip format or just create a zip containing an empty
directory with any zip utility look at the ZipInfo using zipfile.py.

Oren

I reverse engineered a zip file. Looks like I can save an empty
directory by setting zipinfo.external_attr as 48 (at least for Windows
2000).

z = zipfile.ZipFile("new.zip","w")
zinfo = zipfile.ZipInfo("empty/",(2002,12,31,23,59,59))
zinfo.external_attr = 48
z.writestr(zinfo, "")

I guess zipfile should be enhanced to save empty directory too.

Wai Yip Tung
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top