diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2005-03-04 19:40:34 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2005-03-04 19:40:34 (GMT) |
commit | 00a73e7715eb9bba3203aea6caa326f3f54a0d20 (patch) | |
tree | e65218da83a5f6e056ad81086c1c1d260cc1b1cf /Lib | |
parent | fd78a6f7f8c507678238c7264f7426c3148b6a84 (diff) | |
download | cpython-00a73e7715eb9bba3203aea6caa326f3f54a0d20.zip cpython-00a73e7715eb9bba3203aea6caa326f3f54a0d20.tar.gz cpython-00a73e7715eb9bba3203aea6caa326f3f54a0d20.tar.bz2 |
Patch #1043890: tarfile: add extractall() method.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/tarfile.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 06f3ab3..8bce5d0 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1321,6 +1321,47 @@ class TarFile(object): self.members.append(tarinfo) + def extractall(self, path=".", members=None): + """Extract all members from the archive to the current working + directory and set owner, modification time and permissions on + directories afterwards. `path' specifies a different directory + to extract to. `members' is optional and must be a subset of the + list returned by getmembers(). + """ + directories = [] + + if members is None: + members = self + + for tarinfo in members: + if tarinfo.isdir(): + # Extract directory with a safe mode, so that + # all files below can be extracted as well. + try: + os.makedirs(os.path.join(path, tarinfo.name), 0777) + except EnvironmentError: + pass + directories.append(tarinfo) + else: + self.extract(tarinfo, path) + + # Reverse sort directories. + directories.sort(lambda a, b: cmp(a.name, b.name)) + directories.reverse() + + # Set correct owner, mtime and filemode on directories. + for tarinfo in directories: + path = os.path.join(path, tarinfo.name) + try: + self.chown(tarinfo, path) + self.utime(tarinfo, path) + self.chmod(tarinfo, path) + except ExtractError, e: + if self.errorlevel > 1: + raise + else: + self._dbg(1, "tarfile: %s" % e) + def extract(self, member, path=""): """Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately |