Extracting zip files containing directories with ZipFile

Márcio Faustino m.faustino at gmail.com
Sun Apr 12 11:44:45 EDT 2009


Hi,

Does the ZipFile class correctly handles directories contained within
zip files?

For example, calling "extractall" on an archive with only "folder/
file.txt" in it results in an IOError saying "No such file or
directory" for "./folder/file.txt", because it created a file named
"folder" instead of a directory. (I've tested this with version 2.6.1
on Windows XP.) However, changing that method to the following, seems
to solve this particular problem:

#----------
def extractall(self, path = os.path.curdir, members = None, password =
None):
    if members is None:
        members = self.namelist()

    # Place directories first to create them before extracting any
file.
    members.sort()

    for name in members:
        if name.endswith('/'):
            os.makedirs(os.path.join(path, name))
        else:
            self.extract(name, path, password)
#----------

Thanks,



More information about the Python-list mailing list