blob: 018e8b93afc420aa42e3ef2f8ebfe45c88df27d5 [file] [log] [blame]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001\section{\module{tarfile} --- Read and write tar archive files}
2
3\declaremodule{standard}{tarfile}
4\modulesynopsis{Read and write tar-format archive files.}
5\versionadded{2.3}
6
7\moduleauthor{Lars Gust\"abel}{lars@gustaebel.de}
8\sectionauthor{Lars Gust\"abel}{lars@gustaebel.de}
9
10The \module{tarfile} module makes it possible to read and create tar archives.
11Some facts and figures:
12
13\begin{itemize}
14\item reads and writes \module{gzip} and \module{bzip2} compressed archives.
Fred Drake3bbd1152004-01-13 23:41:32 +000015\item creates \POSIX{} 1003.1-1990 compliant or GNU tar compatible archives.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000016\item reads GNU tar extensions \emph{longname}, \emph{longlink} and
17 \emph{sparse}.
18\item stores pathnames of unlimited length using GNU tar extensions.
19\item handles directories, regular files, hardlinks, symbolic links, fifos,
20 character devices and block devices and is able to acquire and
21 restore file information like timestamp, access permissions and owner.
22\item can handle tape devices.
23\end{itemize}
24
25\begin{funcdesc}{open}{\optional{name\optional{, mode
26 \optional{, fileobj\optional{, bufsize}}}}}
27 Return a \class{TarFile} object for the pathname \var{name}.
28 For detailed information on \class{TarFile} objects,
29 see \citetitle{TarFile Objects} (section \ref{tarfile-objects}).
30
31 \var{mode} has to be a string of the form \code{'filemode[:compression]'},
32 it defaults to \code{'r'}. Here is a full list of mode combinations:
33
34 \begin{tableii}{c|l}{code}{mode}{action}
35 \lineii{'r'}{Open for reading with transparent compression (recommended).}
36 \lineii{'r:'}{Open for reading exclusively without compression.}
37 \lineii{'r:gz'}{Open for reading with gzip compression.}
38 \lineii{'r:bz2'}{Open for reading with bzip2 compression.}
39 \lineii{'a' or 'a:'}{Open for appending with no compression.}
40 \lineii{'w' or 'w:'}{Open for uncompressed writing.}
41 \lineii{'w:gz'}{Open for gzip compressed writing.}
42 \lineii{'w:bz2'}{Open for bzip2 compressed writing.}
43 \end{tableii}
44
45 Note that \code{'a:gz'} or \code{'a:bz2'} is not possible.
46 If \var{mode} is not suitable to open a certain (compressed) file for
47 reading, \exception{ReadError} is raised. Use \var{mode} \code{'r'} to
48 avoid this. If a compression method is not supported,
49 \exception{CompressionError} is raised.
50
51 If \var{fileobj} is specified, it is used as an alternative to
52 a file object opened for \var{name}.
53
54 For special purposes, there is a second format for \var{mode}:
Fred Drake3bbd1152004-01-13 23:41:32 +000055 \code{'filemode|[compression]'}. \function{open()} will return a
56 \class{TarFile} object that processes its data as a stream of
57 blocks. No random seeking will be done on the file. If given,
58 \var{fileobj} may be any object that has a \method{read()} or
59 \method{write()} method (depending on the \var{mode}).
60 \var{bufsize} specifies the blocksize and defaults to \code{20 *
61 512} bytes. Use this variant in combination with
62 e.g. \code{sys.stdin}, a socket file object or a tape device.
63 However, such a \class{TarFile} object is limited in that it does
64 not allow to be accessed randomly, see ``Examples''
65 (section~\ref{tar-examples}). The currently possible modes:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000066
Fred Drake3bbd1152004-01-13 23:41:32 +000067 \begin{tableii}{c|l}{code}{Mode}{Action}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000068 \lineii{'r|'}{Open a \emph{stream} of uncompressed tar blocks for reading.}
69 \lineii{'r|gz'}{Open a gzip compressed \emph{stream} for reading.}
70 \lineii{'r|bz2'}{Open a bzip2 compressed \emph{stream} for reading.}
71 \lineii{'w|'}{Open an uncompressed \emph{stream} for writing.}
72 \lineii{'w|gz'}{Open an gzip compressed \emph{stream} for writing.}
73 \lineii{'w|bz2'}{Open an bzip2 compressed \emph{stream} for writing.}
74 \end{tableii}
75\end{funcdesc}
76
77\begin{classdesc*}{TarFile}
78 Class for reading and writing tar archives. Do not use this
79 class directly, better use \function{open()} instead.
Fred Drake3bbd1152004-01-13 23:41:32 +000080 See ``TarFile Objects'' (section~\ref{tarfile-objects}).
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000081\end{classdesc*}
82
83\begin{funcdesc}{is_tarfile}{name}
Fred Drake3bbd1152004-01-13 23:41:32 +000084 Return \constant{True} if \var{name} is a tar archive file, that
85 the \module{tarfile} module can read.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000086\end{funcdesc}
87
88\begin{classdesc}{TarFileCompat}{filename\optional{, mode\optional{,
Fred Drake3bbd1152004-01-13 23:41:32 +000089 compression}}}
90 Class for limited access to tar archives with a
91 \refmodule{zipfile}-like interface. Please consult the
92 documentation of the \refmodule{zipfile} module for more details.
93 \var{compression} must be one of the following constants:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000094 \begin{datadesc}{TAR_PLAIN}
95 Constant for an uncompressed tar archive.
96 \end{datadesc}
97 \begin{datadesc}{TAR_GZIPPED}
Fred Drake3bbd1152004-01-13 23:41:32 +000098 Constant for a \refmodule{gzip} compressed tar archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000099 \end{datadesc}
100\end{classdesc}
101
102\begin{excdesc}{TarError}
103 Base class for all \module{tarfile} exceptions.
104\end{excdesc}
105
106\begin{excdesc}{ReadError}
107 Is raised when a tar archive is opened, that either cannot be handled by
108 the \module{tarfile} module or is somehow invalid.
109\end{excdesc}
110
111\begin{excdesc}{CompressionError}
112 Is raised when a compression method is not supported or when the data
113 cannot be decoded properly.
114\end{excdesc}
115
116\begin{excdesc}{StreamError}
117 Is raised for the limitations that are typical for stream-like
118 \class{TarFile} objects.
119\end{excdesc}
120
121\begin{excdesc}{ExtractError}
122 Is raised for \emph{non-fatal} errors when using \method{extract()}, but
123 only if \member{TarFile.errorlevel}\code{ == 2}.
124\end{excdesc}
125
126\begin{seealso}
Fred Drake3bbd1152004-01-13 23:41:32 +0000127 \seemodule{zipfile}{Documentation of the \refmodule{zipfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000128 standard module.}
129
130 \seetitle[http://www.gnu.org/manual/tar/html_chapter/tar_8.html\#SEC118]
131 {GNU tar manual, Standard Section}{Documentation for tar archive files,
132 including GNU tar extensions.}
133\end{seealso}
134
135%-----------------
136% TarFile Objects
137%-----------------
138
139\subsection{TarFile Objects \label{tarfile-objects}}
140
141The \class{TarFile} object provides an interface to a tar archive. A tar
142archive is a sequence of blocks. An archive member (a stored file) is made up
143of a header block followed by data blocks. It is possible, to store a file in a
144tar archive several times. Each archive member is represented by a
145\class{TarInfo} object, see \citetitle{TarInfo Objects} (section
146\ref{tarinfo-objects}) for details.
147
148\begin{classdesc}{TarFile}{\optional{name
149 \optional{, mode\optional{, fileobj}}}}
150 Open an \emph{(uncompressed)} tar archive \var{name}.
151 \var{mode} is either \code{'r'} to read from an existing archive,
152 \code{'a'} to append data to an existing file or \code{'w'} to create a new
153 file overwriting an existing one. \var{mode} defaults to \code{'r'}.
154
155 If \var{fileobj} is given, it is used for reading or writing data.
156 If it can be determined, \var{mode} is overridden by \var{fileobj}'s mode.
157 \begin{notice}
158 \var{fileobj} is not closed, when \class{TarFile} is closed.
159 \end{notice}
160\end{classdesc}
161
162\begin{methoddesc}{open}{...}
163 Alternative constructor. The \function{open()} function on module level is
Fred Drake3bbd1152004-01-13 23:41:32 +0000164 actually a shortcut to this classmethod. See section~\ref{module-tarfile}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000165 for details.
166\end{methoddesc}
167
168\begin{methoddesc}{getmember}{name}
169 Return a \class{TarInfo} object for member \var{name}. If \var{name} can
170 not be found in the archive, \exception{KeyError} is raised.
171 \begin{notice}
172 If a member occurs more than once in the archive, its last
Johannes Gijsbersd3452252004-09-11 16:50:06 +0000173 occurrence is assumed to be the most up-to-date version.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000174 \end{notice}
175\end{methoddesc}
176
177\begin{methoddesc}{getmembers}{}
178 Return the members of the archive as a list of \class{TarInfo} objects.
179 The list has the same order as the members in the archive.
180\end{methoddesc}
181
182\begin{methoddesc}{getnames}{}
183 Return the members as a list of their names. It has the same order as
184 the list returned by \method{getmembers()}.
185\end{methoddesc}
186
187\begin{methoddesc}{list}{verbose=True}
188 Print a table of contents to \code{sys.stdout}. If \var{verbose} is
Fred Drake3bbd1152004-01-13 23:41:32 +0000189 \constant{False}, only the names of the members are printed. If it is
190 \constant{True}, output similar to that of \program{ls -l} is produced.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000191\end{methoddesc}
192
193\begin{methoddesc}{next}{}
194 Return the next member of the archive as a \class{TarInfo} object, when
195 \class{TarFile} is opened for reading. Return \code{None} if there is no
196 more available.
197\end{methoddesc}
198
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000199\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
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000211\begin{methoddesc}{extract}{member\optional{, path}}
212 Extract a member from the archive to the current working directory,
213 using its full name. Its file information is extracted as accurately as
214 possible.
215 \var{member} may be a filename or a \class{TarInfo} object.
216 You can specify a different directory using \var{path}.
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000217 \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}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000222\end{methoddesc}
223
224\begin{methoddesc}{extractfile}{member}
225 Extract a member from the archive as a file object.
226 \var{member} may be a filename or a \class{TarInfo} object.
227 If \var{member} is a regular file, a file-like object is returned.
228 If \var{member} is a link, a file-like object is constructed from the
229 link's target.
230 If \var{member} is none of the above, \code{None} is returned.
231 \begin{notice}
232 The file-like object is read-only and provides the following methods:
233 \method{read()}, \method{readline()}, \method{readlines()},
234 \method{seek()}, \method{tell()}.
235 \end{notice}
236\end{methoddesc}
237
Fred Drake3bbd1152004-01-13 23:41:32 +0000238\begin{methoddesc}{add}{name\optional{, arcname\optional{, recursive}}}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000239 Add the file \var{name} to the archive. \var{name} may be any type
240 of file (directory, fifo, symbolic link, etc.).
241 If given, \var{arcname} specifies an alternative name for the file in the
242 archive. Directories are added recursively by default.
Fred Drake3bbd1152004-01-13 23:41:32 +0000243 This can be avoided by setting \var{recursive} to \constant{False};
244 the default is \constant{True}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000245\end{methoddesc}
246
247\begin{methoddesc}{addfile}{tarinfo\optional{, fileobj}}
248 Add the \class{TarInfo} object \var{tarinfo} to the archive.
Fred Drake3bbd1152004-01-13 23:41:32 +0000249 If \var{fileobj} is given, \code{\var{tarinfo}.size} bytes are read
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000250 from it and added to the archive. You can create \class{TarInfo} objects
251 using \method{gettarinfo()}.
252 \begin{notice}
253 On Windows platforms, \var{fileobj} should always be opened with mode
254 \code{'rb'} to avoid irritation about the file size.
255 \end{notice}
256\end{methoddesc}
257
Fred Drake3bbd1152004-01-13 23:41:32 +0000258\begin{methoddesc}{gettarinfo}{\optional{name\optional{,
259 arcname\optional{, fileobj}}}}
260 Create a \class{TarInfo} object for either the file \var{name} or
261 the file object \var{fileobj} (using \function{os.fstat()} on its
262 file descriptor). You can modify some of the \class{TarInfo}'s
263 attributes before you add it using \method{addfile()}. If given,
264 \var{arcname} specifies an alternative name for the file in the
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000265 archive.
266\end{methoddesc}
267
268\begin{methoddesc}{close}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000269 Close the \class{TarFile}. In write mode, two finishing zero
270 blocks are appended to the archive.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000271\end{methoddesc}
272
Fred Drake3bbd1152004-01-13 23:41:32 +0000273\begin{memberdesc}{posix}
274 If true, create a \POSIX{} 1003.1-1990 compliant archive. GNU
275 extensions are not used, because they are not part of the \POSIX{}
Neal Norwitzd96d1012004-07-20 22:23:02 +0000276 standard. This limits the length of filenames to at most 256,
277 link names to 100 characters and the maximum file size to 8
278 gigabytes. A \exception{ValueError} is raised if a file exceeds
279 this limit. If false, create a GNU tar compatible archive. It
280 will not be \POSIX{} compliant, but can store files without any
Martin v. Löwis75b9da42004-08-18 13:57:44 +0000281 of the above restrictions.
Neal Norwitz525b3152004-08-20 01:52:42 +0000282 \versionchanged[\var{posix} defaults to \constant{False}]{2.4}
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000283\end{memberdesc}
284
Fred Drake3bbd1152004-01-13 23:41:32 +0000285\begin{memberdesc}{dereference}
286 If false, add symbolic and hard links to archive. If true, add the
287 content of the target files to the archive. This has no effect on
288 systems that do not support symbolic links.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000289\end{memberdesc}
290
Fred Drake3bbd1152004-01-13 23:41:32 +0000291\begin{memberdesc}{ignore_zeros}
292 If false, treat an empty block as the end of the archive. If true,
293 skip empty (and invalid) blocks and try to get as many members as
294 possible. This is only useful for concatenated or damaged
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000295 archives.
296\end{memberdesc}
297
298\begin{memberdesc}{debug=0}
Fred Drake3bbd1152004-01-13 23:41:32 +0000299 To be set from \code{0} (no debug messages; the default) up to
300 \code{3} (all debug messages). The messages are written to
301 \code{sys.stdout}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000302\end{memberdesc}
303
Fred Drake3bbd1152004-01-13 23:41:32 +0000304\begin{memberdesc}{errorlevel}
305 If \code{0} (the default), all errors are ignored when using
306 \method{extract()}. Nevertheless, they appear as error messages
307 in the debug output, when debugging is enabled. If \code{1}, all
308 \emph{fatal} errors are raised as \exception{OSError} or
309 \exception{IOError} exceptions. If \code{2}, all \emph{non-fatal}
310 errors are raised as \exception{TarError} exceptions as well.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000311\end{memberdesc}
312
313%-----------------
314% TarInfo Objects
315%-----------------
316
317\subsection{TarInfo Objects \label{tarinfo-objects}}
318
Fred Drake3bbd1152004-01-13 23:41:32 +0000319A \class{TarInfo} object represents one member in a
320\class{TarFile}. Aside from storing all required attributes of a file
321(like file type, size, time, permissions, owner etc.), it provides
322some useful methods to determine its type. It does \emph{not} contain
323the file's data itself.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000324
Fred Drake3bbd1152004-01-13 23:41:32 +0000325\class{TarInfo} objects are returned by \class{TarFile}'s methods
326\method{getmember()}, \method{getmembers()} and \method{gettarinfo()}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000327
328\begin{classdesc}{TarInfo}{\optional{name}}
329 Create a \class{TarInfo} object.
330\end{classdesc}
331
332\begin{methoddesc}{frombuf}{}
333 Create and return a \class{TarInfo} object from a string buffer.
334\end{methoddesc}
335
336\begin{methoddesc}{tobuf}{}
337 Create a string buffer from a \class{TarInfo} object.
338\end{methoddesc}
339
340A \code{TarInfo} object has the following public data attributes:
Fred Drake3bbd1152004-01-13 23:41:32 +0000341
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000342\begin{memberdesc}{name}
343 Name of the archive member.
344\end{memberdesc}
345
346\begin{memberdesc}{size}
347 Size in bytes.
348\end{memberdesc}
349
350\begin{memberdesc}{mtime}
351 Time of last modification.
352\end{memberdesc}
353
354\begin{memberdesc}{mode}
355 Permission bits.
356\end{memberdesc}
357
358\begin{memberdesc}{type}
Fred Drake3bbd1152004-01-13 23:41:32 +0000359 File type. \var{type} is usually one of these constants:
360 \constant{REGTYPE}, \constant{AREGTYPE}, \constant{LNKTYPE},
361 \constant{SYMTYPE}, \constant{DIRTYPE}, \constant{FIFOTYPE},
362 \constant{CONTTYPE}, \constant{CHRTYPE}, \constant{BLKTYPE},
363 \constant{GNUTYPE_SPARSE}. To determine the type of a
364 \class{TarInfo} object more conveniently, use the \code{is_*()}
365 methods below.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000366\end{memberdesc}
367
368\begin{memberdesc}{linkname}
Fred Drake3bbd1152004-01-13 23:41:32 +0000369 Name of the target file name, which is only present in
370 \class{TarInfo} objects of type \constant{LNKTYPE} and
371 \constant{SYMTYPE}.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000372\end{memberdesc}
373
Fred Drake3bbd1152004-01-13 23:41:32 +0000374\begin{memberdesc}{uid}
375 User ID of the user who originally stored this member.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000376\end{memberdesc}
377
Fred Drake3bbd1152004-01-13 23:41:32 +0000378\begin{memberdesc}{gid}
379 Group ID of the user who originally stored this member.
380\end{memberdesc}
381
382\begin{memberdesc}{uname}
383 User name.
384\end{memberdesc}
385
386\begin{memberdesc}{gname}
387 Group name.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000388\end{memberdesc}
389
390A \class{TarInfo} object also provides some convenient query methods:
Fred Drake3bbd1152004-01-13 23:41:32 +0000391
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000392\begin{methoddesc}{isfile}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000393 Return \constant{True} if the \class{Tarinfo} object is a regular
394 file.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000395\end{methoddesc}
396
397\begin{methoddesc}{isreg}{}
398 Same as \method{isfile()}.
399\end{methoddesc}
400
401\begin{methoddesc}{isdir}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000402 Return \constant{True} if it is a directory.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000403\end{methoddesc}
404
405\begin{methoddesc}{issym}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000406 Return \constant{True} if it is a symbolic link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000407\end{methoddesc}
408
409\begin{methoddesc}{islnk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000410 Return \constant{True} if it is a hard link.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000411\end{methoddesc}
412
413\begin{methoddesc}{ischr}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000414 Return \constant{True} if it is a character device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000415\end{methoddesc}
416
417\begin{methoddesc}{isblk}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000418 Return \constant{True} if it is a block device.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000419\end{methoddesc}
420
421\begin{methoddesc}{isfifo}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000422 Return \constant{True} if it is a FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000423\end{methoddesc}
424
425\begin{methoddesc}{isdev}{}
Fred Drake3bbd1152004-01-13 23:41:32 +0000426 Return \constant{True} if it is one of character device, block
427 device or FIFO.
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000428\end{methoddesc}
429
430%------------------------
431% Examples
432%------------------------
433
434\subsection{Examples \label{tar-examples}}
435
Martin v. Löwis00a73e72005-03-04 19:40:34 +0000436How to extract an entire tar archive to the current working directory:
437\begin{verbatim}
438import tarfile
439tar = tarfile.open("sample.tar.gz")
440tar.extractall()
441tar.close()
442\end{verbatim}
443
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000444How to create an uncompressed tar archive from a list of filenames:
445\begin{verbatim}
446import tarfile
447tar = tarfile.open("sample.tar", "w")
448for name in ["foo", "bar", "quux"]:
449 tar.add(name)
450tar.close()
451\end{verbatim}
452
453How to read a gzip compressed tar archive and display some member information:
454\begin{verbatim}
455import tarfile
456tar = tarfile.open("sample.tar.gz", "r:gz")
457for tarinfo in tar:
458 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
459 if tarinfo.isreg():
460 print "a regular file."
461 elif tarinfo.isdir():
462 print "a directory."
463 else:
464 print "something else."
465tar.close()
466\end{verbatim}
467
468How to create a tar archive with faked information:
469\begin{verbatim}
470import tarfile
471tar = tarfile.open("sample.tar.gz", "w:gz")
472for name in namelist:
473 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
474 tarinfo.uid = 123
475 tarinfo.gid = 456
476 tarinfo.uname = "johndoe"
477 tarinfo.gname = "fake"
478 tar.addfile(tarinfo, file(name))
479tar.close()
480\end{verbatim}
481
482The \emph{only} way to extract an uncompressed tar stream from
483\code{sys.stdin}:
484\begin{verbatim}
485import sys
486import tarfile
487tar = tarfile.open(mode="r|", fileobj=sys.stdin)
488for tarinfo in tar:
489 tar.extract(tarinfo)
490tar.close()
491\end{verbatim}