473,813 Members | 4,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15223
Tung Wai Yip <tu********@yah oo.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********@yah oo.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********@yah oo.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.writest r 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*******@hish ome.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.writest r 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_at tr = 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*******@hish ome.net> wrote:
On Thu, Jun 26, 2003 at 04:31:37AM +0000, JanC wrote:
Tung Wai Yip <tu********@yah oo.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.writest r it should probably work. Consult the
documentatio n 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.externa l_attr as 48 (at least for Windows
2000).

z = zipfile.ZipFile ("new.zip"," w")
zinfo = zipfile.ZipInfo ("empty/",(2002,12,31,2 3,59,59))
zinfo.external_ attr = 48
z.writestr(zinf o, "")

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
2975
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? Thank you in advance! Best regards Mark
2
2217
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 if I try to add empty directories an exception is trown. Thanks.
7
2142
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. That is bothersome if I want to install the script on other machines. The same for pkwares zip. At least I cannot seem to find a free version of it. gzip seems able to do the trick, but I need to install Cygwin, that's
1
2050
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 an empty directory - you need to give it a filename. I know the zip format will support it as winzip will allow it... I looked at the zipfile module source - and I'm sure it's not impenetrable but I don't wish to learn the intricasies of the zip...
1
1603
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 = open(diretorio + element + '/',"w")
2
3455
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 nice, except the zipfile created contains the directory path of the source file -- which I do NOT want to recreate. E.g. when I compress 'c:\\temp\\zz.txt', the resulting zipfile contains not just 'zz.txt' file, but also the whole directory, i.e....
1
1540
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
33676
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 this? Thanks~
10
38619
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 zip a flat number of files in one folder, but I am having heck of a time trying to zip the whole directory. I want to zip it all to a file called "help.zip", and I want it to retain the original file structure. Of my several tries, here is the...
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10406
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10420
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9221
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.