Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   add empty directory using zipfile? (http://www.velocityreviews.com/forums/t318840-add-empty-directory-using-zipfile.html)

Tung Wai Yip 06-25-2003 05:29 PM

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

tung

JanC 06-26-2003 04:31 AM

Re: add empty directory using zipfile?
 
Tung Wai Yip <tungwaiyip@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

Ben Finney 06-26-2003 04:50 AM

Re: add empty directory using zipfile?
 
On Thu, 26 Jun 2003 04:31:37 GMT, JanC wrote:
> Tung Wai Yip <tungwaiyip@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

Oren Tirosh 06-26-2003 07:40 AM

Re: add empty directory using zipfile?
 
On Thu, Jun 26, 2003 at 04:31:37AM +0000, JanC wrote:
> Tung Wai Yip <tungwaiyip@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


JanC 06-26-2003 01:08 PM

Re: add empty directory using zipfile?
 
Ben Finney <bignose-hates-spam@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

JanC 06-26-2003 08:19 PM

Re: add empty directory using zipfile?
 
Oren Tirosh <oren-py-l@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

Tung Wai Yip 06-26-2003 09:39 PM

Re: add empty directory using zipfile?
 
On Thu, 26 Jun 2003 03:40:01 -0400, Oren Tirosh
<oren-py-l@hishome.net> wrote:

>On Thu, Jun 26, 2003 at 04:31:37AM +0000, JanC wrote:
>> Tung Wai Yip <tungwaiyip@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



All times are GMT. The time now is 04:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.