Skip to content

Commit 9361683

Browse files
author
loewis
committed
Patch #1043890: tarfile: add extractall() method.
git-svn-id: http://svn.python.org/projects/python/trunk@38572 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent a9b1700 commit 9361683

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

Doc/lib/libtarfile.tex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,29 @@ \subsection{TarFile Objects \label{tarfile-objects}}
196196
more available.
197197
\end{methoddesc}
198198

199+
\begin{methoddesc}{extractall}{\optional{path\optional{, members}}}
200+
Extract all members from the archive to the current working directory
201+
or directory \var{path}. If optional \var{members} is given, it must be
202+
a subset of the list returned by \method{getmembers()}.
203+
Directory informations like owner, modification time and permissions are
204+
set after all members have been extracted. This is done to work around two
205+
problems: A directory's modification time is reset each time a file is
206+
created in it. And, if a directory's permissions do not allow writing,
207+
extracting files to it will fail.
208+
\versionadded{2.5}
209+
\end{methoddesc}
210+
199211
\begin{methoddesc}{extract}{member\optional{, path}}
200212
Extract a member from the archive to the current working directory,
201213
using its full name. Its file information is extracted as accurately as
202214
possible.
203215
\var{member} may be a filename or a \class{TarInfo} object.
204216
You can specify a different directory using \var{path}.
217+
\begin{notice}
218+
Because the \method{extract()} method allows random access to a tar
219+
archive there are some issues you must take care of yourself. See the
220+
description for \method{extractall()} above.
221+
\end{notice}
205222
\end{methoddesc}
206223

207224
\begin{methoddesc}{extractfile}{member}
@@ -416,6 +433,14 @@ \subsection{TarInfo Objects \label{tarinfo-objects}}
416433

417434
\subsection{Examples \label{tar-examples}}
418435

436+
How to extract an entire tar archive to the current working directory:
437+
\begin{verbatim}
438+
import tarfile
439+
tar = tarfile.open("sample.tar.gz")
440+
tar.extractall()
441+
tar.close()
442+
\end{verbatim}
443+
419444
How to create an uncompressed tar archive from a list of filenames:
420445
\begin{verbatim}
421446
import tarfile

Lib/tarfile.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,47 @@ def addfile(self, tarinfo, fileobj=None):
13211321

13221322
self.members.append(tarinfo)
13231323

1324+
def extractall(self, path=".", members=None):
1325+
"""Extract all members from the archive to the current working
1326+
directory and set owner, modification time and permissions on
1327+
directories afterwards. `path' specifies a different directory
1328+
to extract to. `members' is optional and must be a subset of the
1329+
list returned by getmembers().
1330+
"""
1331+
directories = []
1332+
1333+
if members is None:
1334+
members = self
1335+
1336+
for tarinfo in members:
1337+
if tarinfo.isdir():
1338+
# Extract directory with a safe mode, so that
1339+
# all files below can be extracted as well.
1340+
try:
1341+
os.makedirs(os.path.join(path, tarinfo.name), 0777)
1342+
except EnvironmentError:
1343+
pass
1344+
directories.append(tarinfo)
1345+
else:
1346+
self.extract(tarinfo, path)
1347+
1348+
# Reverse sort directories.
1349+
directories.sort(lambda a, b: cmp(a.name, b.name))
1350+
directories.reverse()
1351+
1352+
# Set correct owner, mtime and filemode on directories.
1353+
for tarinfo in directories:
1354+
path = os.path.join(path, tarinfo.name)
1355+
try:
1356+
self.chown(tarinfo, path)
1357+
self.utime(tarinfo, path)
1358+
self.chmod(tarinfo, path)
1359+
except ExtractError, e:
1360+
if self.errorlevel > 1:
1361+
raise
1362+
else:
1363+
self._dbg(1, "tarfile: %s" % e)
1364+
13241365
def extract(self, member, path=""):
13251366
"""Extract a member from the archive to the current working directory,
13261367
using its full name. Its file information is extracted as accurately

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Extension Modules
7575
Library
7676
-------
7777

78+
- Patch #1043890: Add extractall method to tarfile.
79+
7880
- Patch #1075887: Don't require MSVC in distutils if there is nothing
7981
to build.
8082

0 commit comments

Comments
 (0)