473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 18 '05 #1
6 15118
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Wondratschek | last post by:
Hi! I hope it's a simple question: How can I pack empty directories? I want to mirror a whole directory structure in an archive. So I want to zip non-empty AND empty directories. Any ideas?...
2
by: T. Kaufmann | last post by:
Hi there, in my directory are some 'normal' files like *.py sources and some empty directories too. How can I put all togehter into a zip-archive? There is no problem to zip the *.py files but...
7
by: Max M | last post by:
I guess that the best approach is calling a shell tool with something like os.popen(). But I cannot seem to find any free tools. Winzip has a command line option, but for registered users only....
1
by: Fuzzyman | last post by:
I have created a set of classes that will profile a file structure and record all changes as a simple markup and single zipfile of all new/modified files. I can't get the zipfile module to archive...
1
by: ralobao | last post by:
I have this code: try: file = zipfile.ZipFile(nome_arquivo) Gauge.start() #inicia o Gauge for element in file.namelist(): try: newFile = open(diretorio + element,"wb") except: newFile =...
2
by: Bulba! | last post by:
Hello everyone, I'm a newbie to Python, learning it and tried to write the script that would read file, zip it, and put in the target directory. I used ZipFile module (code below). It works...
1
by: iamlevis3 | last post by:
Does Python have any internal facility for creating recursive archives of a directory? I'd like to avoid reliance on extenal tools (winzip,tar,etc). Thanks!
13
by: could ildg | last post by:
I want to check if a folder named "foldername" is empty. I use os.listdir(foldername)== to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do...
10
by: Bertsche | last post by:
I'm relatively new to python, and am trying to zip a directory containing several levels of files and folders. I can use the walk function to name each file in my directory, and I can use zipfile to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.