By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

add empty directory using zipfile?

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

tung
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tung Wai Yip <tu********@yahoo.com> schreef:
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.

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
Jul 18 '05 #2

P: n/a
On Thu, 26 Jun 2003 04:31:37 GMT, JanC wrote:
Tung Wai Yip <tu********@yahoo.com> schreef:
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.


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.

--
\ "Timid men prefer the calm of despotism to the boisterous sea |
`\ of liberty." -- Thomas Jefferson |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #3

P: n/a
On Thu, Jun 26, 2003 at 04:31:37AM +0000, JanC wrote:
Tung Wai Yip <tu********@yahoo.com> schreef:
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.


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

Jul 18 '05 #4

P: n/a
Ben Finney <bi****************@and-zip-does-too.com.au> schreef:
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...

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
Jul 18 '05 #5

P: n/a
Oren Tirosh <or*******@hishome.net> schreef:
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...

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
Jul 18 '05 #6

P: n/a
On Thu, 26 Jun 2003 03:40:01 -0400, Oren Tirosh
<or*******@hishome.net> wrote:
On Thu, Jun 26, 2003 at 04:31:37AM +0000, JanC wrote:
Tung Wai Yip <tu********@yahoo.com> schreef:
> 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.


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

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.