Extracting zip files containing directories with ZipFile

Discussion in 'Python' started by Márcio Faustino, Apr 12, 2009.

  1. 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,
    Márcio Faustino, Apr 12, 2009
    #1
    1. Advertising

  2. Márcio Faustino

    MRAB Guest

    Márcio Faustino wrote:
    > 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)
    > #----------
    >

    Strictly speaking, zip files don't contain nested folders, but only a
    flat list of files.

    However, by convention a folder hierarchy is shown by the use of slashes
    in the names eg. "foo/bar.txt" is a folder "foo" containing a file
    "bar.txt". You can also represent an empty folder with a (zero-length)
    file ending with a slash, eg "foo/" is an empty folder "foo".

    Just create any intermediate folders on demand if they don't already
    exist.
    MRAB, Apr 12, 2009
    #2
    1. Advertising

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


    That depends on the version of Python you are using. See
    http://bugs.python.org/4710

    Regards,
    Martin
    Martin v. Löwis, Apr 12, 2009
    #3
  4. On Apr 12, 5:01 pm, MRAB <> wrote:
    > Strictly speaking, zip files don't contain nested folders, but only a
    > flat list of files.


    Didn't know that, thanks.

    On Apr 12, 8:01 pm, "Martin v. Löwis" <> wrote:
    > That depends on the version of Python you are using. Seehttp://bugs.python.org/4710


    Thanks. :)
    Márcio Faustino, Apr 12, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Wondratschek

    Python zipfile: Zip empty directories.

    Mark Wondratschek, Aug 29, 2003, in forum: Python
    Replies:
    2
    Views:
    622
    Mark Wondratschek
    Sep 1, 2003
  2. could ildg
    Replies:
    0
    Views:
    465
    could ildg
    Jul 1, 2005
  3. Replies:
    3
    Views:
    330
    Dave Angel
    Oct 17, 2009
  4. Graham Wood
    Replies:
    3
    Views:
    537
    A. Sinan Unur
    Jan 11, 2004
  5. Ron Smith
    Replies:
    5
    Views:
    146
    Michele Dondi
    Nov 2, 2004
Loading...

Share This Page