tarfile --- tar ã¢ãŒã«ã€ããã¡ã€ã«ã®èªã¿æžã¶
ãœãŒã¹ã³ãŒã: Lib/tarfile.py
The tarfile module makes it possible to read and write tar
archives, including those using gzip, bz2 and lzma compression.
Use the zipfile module to read or write .zip files, or the
higher-level functions in shutil.
ããã€ãã®äºå®ãšåœ¢æ :
ã¢ãžã¥ãŒã«ãå©çšå¯èœãªå Žåã
gzipãbz2ãcompression.zstdãªãã³ã«lzmaã§å§çž®ãããã¢ãŒã«ã€ããèªã¿æžãããŸããIf any of these optional modules are missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see ãªãã·ã§ã³ã®ã¢ãžã¥ãŒã«ã®èŠä»¶.
POSIX.1-1988 (ustar) ãã©ãŒãããã®èªã¿æžãããµããŒãããŠããŸãã
longname ããã³ longlink æ¡åŒµãå«ã GNU tar ãã©ãŒãããã®èªã¿æžãããµããŒãããŠããŸããã¹ããŒã¹ãã¡ã€ã«ã®åŸ©å ãå«ã sparse æ¡åŒµã¯èªã¿èŸŒã¿ã®ã¿ãµããŒãããŠããŸãã
POSIX.1-2001 (pax) ãã©ãŒãããã®èªã¿æžãããµããŒãããŠããŸãã
ãã£ã¬ã¯ããªãäžè¬ãã¡ã€ã«ãããŒããªã³ã¯ãã·ã³ããªãã¯ãªã³ã¯ãfifoããã£ã©ã¯ã¿ãŒããã€ã¹ããã³ãããã¯ããã€ã¹ãåŠçããŸãããŸããã¿ã€ã ã¹ã¿ã³ããã¢ã¯ã»ã¹èš±å¯ãææè ã®ãããªãã¡ã€ã«æ å ±ã®ååŸããã³ä¿åãå¯èœã§ãã
ããŒãžã§ã³ 3.3 ã§å€æŽ: lzma å§çž®ããµããŒãããŸããã
ããŒãžã§ã³ 3.12 ã§å€æŽ: Archives are extracted using a filter, which makes it possible to either limit surprising/dangerous features, or to acknowledge that they are expected and the archive is fully trusted.
ããŒãžã§ã³ 3.14 ã§å€æŽ: Set the default extraction filter to data,
which disallows some dangerous features such as links to absolute paths
or paths outside of the destination. Previously, the filter strategy
was equivalent to fully_trusted.
ããŒãžã§ã³ 3.14 ã§å€æŽ: Added support for Zstandard compression using compression.zstd.
- tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)¶
ãã¹å name ã®
TarFileãªããžã§ã¯ããè¿ããŸããTarFileãªããžã§ã¯ããšãå©çšã§ããããŒã¯ãŒãåŒæ°ã«é¢ããè©³çŽ°ãªæ å ±ã«ã€ããŠã¯ãTarFile ãªããžã§ã¯ã ç¯ãåç §ããŠãã ãããmode ã¯
'filemode[:compression]'ã®åœ¢åŒããšãæååã§ãªããã°ãªããŸãããããã©ã«ãã®å€ã¯'r'ã§ãã以äžã« mode ã®ãšãããçµã¿åãããã¹ãŠã瀺ããŸã:mode
action
'r'ãŸãã¯'r:*'å§çž®æ¹æ³ã«é¢ããŠééçã«ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããŸã (æšå¥š)ã
'r:'éå§çž®ã§èªã¿èŸŒã¿çšã«æä»çã«ãªãŒãã³ããŸãã
'r:gz'gzip å§çž®ã§èªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r:bz2'bzip2 å§çž®ã§èªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r:xz'lzma å§çž®ã§èªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r:zst'Zstandard å§çž®ã§èªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'x'or'x:'å§çž®ããã« tarfile ãæä»çã«äœæããŸããtarfile ãæ¢åã®å Žå
FileExistsErroräŸå€ãéåºããŸãã'x:gz'gzip å§çž®ã§ tarfile ãäœæããŸããtarfile ãæ¢åã®å Žå
FileExistsErroräŸå€ãéåºããŸãã'x:bz2'bzip2 å§çž®ã§ tarfile ãäœæããŸããtarfile ãæ¢åã®å Žå
FileExistsErroräŸå€ãéåºããŸãã'x:xz'lzma å§çž®ã§ tarfile ãäœæããŸããtarfile ãæ¢åã®å Žå
FileExistsErroräŸå€ãéåºããŸãã'x:zst'Zstandard å§çž®ã§ tarfile ãäœæããŸããtarfile ãæ¢åã®å Žå
FileExistsErroräŸå€ãéåºããŸãã'a'ãŸãã¯'a:'éå§çž®ã§è¿œèšçšã«ãªãŒãã³ããŸãããã¡ã€ã«ãååšããªãå Žåã¯æ°ãã«äœæãããŸãã
'w'ãŸãã¯'w:'éå§çž®ã§æžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w:gz'gzip å§çž®ã§æžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w:bz2'bzip2 å§çž®ã§æžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w:xz'lzma å§çž®ã§æžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w:zst'Zstandard å§çž®ã§æžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'a:gz'ã'a:bz2'ã'a:xz'ã¯å©çšã§ããªãããšã«æ³šæããŠäžããããã mode ãããã (å§çž®ãã) ãã¡ã€ã«ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããã®ã«é©ããŠããªããªããReadErrorãéåºãããŸãããããé²ãã«ã¯ mode'r'ã䜿ã£ãŠäžãããããå§çž®æ¹åŒããµããŒããããŠããªããã°ãCompressionErrorãéåºãããŸãããã fileobj ãæå®ãããŠããã°ããã㯠name ã§ãã€ããªã¢ãŒãã§ãªãŒãã³ããã ãã¡ã€ã«ãªããžã§ã¯ã ã®ä»£æ¿ãšããŠäœ¿ãããšãã§ããŸãããã®ãã¡ã€ã«ãªããžã§ã¯ãã®äœçœ®ã 0 ã§ããããšãåæã«åäœããŸãã
For modes
'w:gz','x:gz','w|gz','w:bz2','x:bz2','w|bz2',tarfile.open()accepts the keyword argument compresslevel (default9) to specify the compression level of the file.'w:xz'ã'x:xz'ããã³'w|xz'ã¢ãŒãã®å Žåãtarfile.open()ã¯ãã¡ã€ã«ã®å§çž®ã¬ãã«ãæå®ããããŒã¯ãŒãåŒæ° preset ãåãä»ããŸããFor modes
'w:zst','x:zst'and'w|zst',tarfile.open()accepts the keyword argument level to specify the compression level of the file. The keyword argument options may also be passed, providing advanced Zstandard compression parameters described byCompressionParameter. The keyword argument zstd_dict can be passed to provide aZstdDict, a Zstandard dictionary used to improve compression of smaller amounts of data.For special purposes, there is a second format for mode:
'filemode|[compression]'.tarfile.open()will return aTarFileobject that processes its data as a stream of blocks. No random seeking will be done on the file. If given, fileobj may be any object that has aread()orwrite()method (depending on the mode) that works with bytes. bufsize specifies the blocksize and defaults to20 * 512bytes. Use this variant in combination with e.g.sys.stdin.buffer, a socket file object or a tape device. However, such aTarFileobject is limited in that it does not allow random access, see 䜿çšäŸ. The currently possible modes:ã¢ãŒã
åäœ
'r|*'tar ãããã¯ã® stream ãå§çž®æ¹æ³ã«é¢ããŠééçã«èªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r|'éå§çž® tar ãããã¯ã® stream ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r|gz'gzip å§çž®ã® stream ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r|bz2'bzip2 å§çž®ã® stream ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r|xz'lzma å§çž®ã® stream ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'r|zst'Zstandard å§çž®ã® stream ãèªã¿èŸŒã¿çšã«ãªãŒãã³ããŸãã
'w|'éå§çž®ã® stream ãæžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w|gz'gzip å§çž®ã® stream ãæžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w|bz2'bzip2 å§çž®ã® stream ãæžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w|xz'lzma å§çž®ã® stream ãæžã蟌ã¿çšã«ãªãŒãã³ããŸãã
'w|zst'Zstandard å§çž®ã® stream ãæžã蟌ã¿çšã«ãªãŒãã³ããŸãã
ããŒãžã§ã³ 3.5 ã§å€æŽ:
'x'(æä»çäœæ) ã¢ãŒãã远å ãããŸãããããŒãžã§ã³ 3.6 ã§å€æŽ: name ãã©ã¡ã¿ã path-like object ãåãä»ããããã«ãªããŸããã
ããŒãžã§ã³ 3.12 ã§å€æŽ: The compresslevel keyword argument also works for streams.
ããŒãžã§ã³ 3.14 ã§å€æŽ: The preset keyword argument also works for streams.
- class tarfile.TarFile
tar ã¢ãŒã«ã€ããèªã¿æžãããããã®ã¯ã©ã¹ã§ãããã®ã¯ã©ã¹ãçŽæ¥äœ¿ããªãããš: 代ããã«
tarfile.open()ã䜿ã£ãŠãã ãããTarFile ãªããžã§ã¯ã ãåç §ããŠãã ããã
- tarfile.is_tarfile(name)¶
Return
Trueif name is a tar archive file, that thetarfilemodule can read. name may be astr, file, or file-like object.ããŒãžã§ã³ 3.9 ã§å€æŽ: ãã¡ã€ã«ããã³ãã¡ã€ã«ã©ã€ã¯ãªããžã§ã¯ãããµããŒãããŸããã
The tarfile module defines the following exceptions:
- exception tarfile.TarError¶
Base class for all
tarfileexceptions.
- exception tarfile.ReadError¶
Is raised when a tar archive is opened, that either cannot be handled by the
tarfilemodule or is somehow invalid.
- exception tarfile.CompressionError¶
å§çž®æ¹æ³ããµããŒããããŠããªããããããã¯ããŒã¿ãæ£ãããã³ãŒãã§ããªãæã«éåºãããŸãã
- exception tarfile.StreamError¶
ã¹ããªãŒã ã®ãããª
TarFileãªããžã§ã¯ãã§å žåçãªå¶éã®ããã«éåºãããŸãã
- exception tarfile.ExtractError¶
TarFile.extract()ã䜿ã£ãæã« èŽåœçã§ãªã ãšã©ãŒã«å¯ŸããŠéåºãããŸãããã ãTarFile.errorlevel== 2ã®å Žåã«éããŸãã
- exception tarfile.HeaderError¶
TarInfo.frombuf()ã¡ãœãããååŸãããããã¡ãŒãäžæ£ã ã£ããšãã«éåºãããŸãã
- exception tarfile.AbsolutePathError¶
Raised to refuse extracting a member with an absolute path.
- exception tarfile.OutsideDestinationError¶
Raised to refuse extracting a member outside the destination directory.
- exception tarfile.SpecialFileError¶
Raised to refuse extracting a special file (e.g. a device or pipe).
- exception tarfile.AbsoluteLinkError¶
Raised to refuse extracting a symbolic link with an absolute path.
- exception tarfile.LinkOutsideDestinationError¶
Raised to refuse extracting a symbolic link pointing outside the destination directory.
- exception tarfile.LinkFallbackError¶
Raised to refuse emulating a link (hard or symbolic) by extracting another archive member, when that member would be rejected by the filter location. The exception that was raised to reject the replacement member is available as
BaseException.__context__.Added in version 3.14.
ã¢ãžã¥ãŒã«ã¬ãã«ã§ä»¥äžã®å®æ°ãå©çšã§ããŸãã
- tarfile.ENCODING¶
æ¢å®ã®æåãšã³ã³ãŒãã£ã³ã°ãWindows ã§ã¯
'utf-8'ããã以å€ã§ã¯sys.getfilesystemencoding()ã®è¿ãå€ã§ãã
Each of the following constants defines a tar archive format that the
tarfile module is able to create. See section ãµããŒãããŠãã tar ãã©ãŒããã for
details.
- tarfile.USTAR_FORMAT¶
POSIX.1-1988 (ustar) ãã©ãŒãããã
- tarfile.GNU_FORMAT¶
GNU tar ãã©ãŒãããã
- tarfile.PAX_FORMAT¶
POSIX.1-2001 (pax) ãã©ãŒãããã
- tarfile.DEFAULT_FORMAT¶
ã¢ãŒã«ã€ããäœæããéã®ããã©ã«ãã®ãã©ãŒããããçŸåšã¯
PAX_FORMATã§ããããŒãžã§ã³ 3.8 ã§å€æŽ: æ°ããã¢ãŒã«ã€ãã®ããã©ã«ããã©ãŒãããã
GNU_FORMATããPAX_FORMATã«å€æŽãããŸããã
åè
zipfileã¢ãžã¥ãŒã«zipfileæšæºã¢ãžã¥ãŒã«ã®ããã¥ã¡ã³ãã- ã¢ãŒã«ã€ãåæäœ
shutilãæäŸãããã髿°Žæºã®ã¢ãŒã«ã€ãæ©èœã«ã€ããŠã®ããã¥ã¡ã³ãã- GNU tar manual, Basic Tar Format
GNU tar æ¡åŒµæ©èœãå«ããtar ã¢ãŒã«ã€ããã¡ã€ã«ã®ããã®ããã¥ã¡ã³ãã
TarFile ãªããžã§ã¯ã¶
TarFile ãªããžã§ã¯ãã¯ãtar ã¢ãŒã«ã€ããžã®ã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããŸããtar ã¢ãŒã«ã€ãã¯äžé£ã®ãããã¯ã§ããã¢ãŒã«ã€ãã¡ã³ã㌠(ä¿åããããã¡ã€ã«) ã¯ãããããŒãããã¯ãšããã«ç¶ãããŒã¿ãããã¯ã§æ§æãããŠããŸããäžã€ã® tar ã¢ãŒã«ã€ãã«ãã¡ã€ã«ãäœåãä¿åããããšãã§ããŸããåã¢ãŒã«ã€ãã¡ã³ããŒã¯ãTarInfo ãªããžã§ã¯ãã§ç¢ºèªã§ããŸãã詳现ã«ã€ããŠã¯ TarInfo ãªããžã§ã¯ã ãåç
§ããŠãã ããã
TarFile ãªããžã§ã¯ã㯠with æã®ã³ã³ããã¹ããããŒãžã£ãŒãšããŠå©çšã§ããŸããwith ãããã¯ãçµäºãããšãã«ãªããžã§ã¯ãã¯ã¯ããŒãºãããŸããäŸå€ãçºçããæãå
éšã§å©çšãããŠãããã¡ã€ã«ãªããžã§ã¯ãã®ã¿ãã¯ããŒãºãããæžã蟌ã¿çšã«ãªãŒãã³ãããã¢ãŒã«ã€ãã®ãã¡ã€ãã©ã€ãºã¯è¡ãããªãããšã«æ³šæããŠãã ããã䜿çšäŸ ç¯ã®ãŠãŒã¹ã±ãŒã¹ãåç
§ããŠãã ããã
Added in version 3.2: ã³ã³ããã¹ã管çã®ãããã³ã«ããµããŒããããŸããã
- class tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors='surrogateescape', pax_headers=None, debug=0, errorlevel=1, stream=False)¶
以äžã®ãã¹ãŠã®åŒæ°ã¯ãªãã·ã§ã³ã§ãã€ã³ã¹ã¿ã³ã¹å±æ§ãšããŠãã¢ã¯ã»ã¹ã§ããŸãã
name is the pathname of the archive. name may be a path-like object. It can be omitted if fileobj is given. In this case, the file object's
nameattribute is used if it exists.mode ã¯ãæ¢åã®ã¢ãŒã«ã€ãããèªã¿èŸŒãããã®
'r'ãæ¢åã®ã¢ãŒã«ã€ãã«è¿œèšããããã®'a'ãæ¢åã®ãã¡ã€ã«ãããã°äžæžãããŠæ°ãããã¡ã€ã«ãäœæãã'w'ããããã¯ååšããªãå Žåã«ã®ã¿æ°ãããã¡ã€ã«ãäœæãã'x'ã®ããããã§ããfileobj ãäžããããŠããã°ãããã䜿ã£ãŠããŒã¿ãèªã¿æžãããŸãããããããæ±ºå®ã§ããã°ãmode 㯠fileobj ã®ã¢ãŒãã§äžæžããããŸããfileobj ã¯äœçœ® 0 ããå©çšãããŸãã
泚é
TarFileãã¯ããŒãºããæãfileobj ã¯ã¯ããŒãºãããŸãããformat ã¯ã¢ãŒã«ã€ãã®æžã蟌ã¿ãã©ãŒããããå¶åŸ¡ããŸããã¢ãžã¥ãŒã«ã¬ãã«ã§å®çŸ©ãããŠããã
USTAR_FORMATãGNU_FORMATããããã¯PAX_FORMATã®ããããã§ããå¿ èŠããããŸãã èªã¿åºãã®ãšãã¯ã1 ã€ã®ã¢ãŒã«ã€ãã«ç°ãªããã©ãŒããããæ··åšããŠãããšããŠãããã©ãŒãããã¯èªåçã«æ€ç¥ãããŸããtarinfo åŒæ°ãå©çšããŠãããã©ã«ãã®
TarInfoã¯ã©ã¹ãå¥ã®ã¯ã©ã¹ã§çœ®ãæããããšãã§ããŸããdereference ã
Falseã ã£ãå Žåãã·ã³ããªãã¯ãªã³ã¯ãããŒããªã³ã¯ãã¢ãŒã«ã€ãã«è¿œå ãããŸããTrueã ã£ãå Žåããªã³ã¯ã®ã¿ãŒã²ãããšãªããã¡ã€ã«ã®å 容ãã¢ãŒã«ã€ãã«è¿œå ãããŸããã·ã³ããªãã¯ãªã³ã¯ããµããŒãããŠããªãã·ã¹ãã ã§ã¯å¹æããããŸãããignore_zeros ã
Falseã ã£ãå Žåã空ãããã¯ãã¢ãŒã«ã€ãã®çµç«¯ãšããŠæ±ããŸããTrueã ã£ãå Žåã空㮠(ç¡å¹ãª) ãããã¯ãã¹ãããããŠãå¯èœãªéãå€ãã®ã¡ã³ããŒãååŸããããšããŸãããã®ãªãã·ã§ã³ã¯ãé£çµãããããå£ããã¢ãŒã«ã€ããã¡ã€ã«ãæ±ããšãã«ã®ã¿ãæå³ããããŸããdebug ã¯
0(ãããã°ã¡ãã»ãŒãžç¡ã) ãã3(å šãããã°ã¡ãã»ãŒãž) ãŸã§èšå®ã§ããŸãããã®ã¡ãã»ãŒãžã¯sys.stderrã«æžã蟌ãŸããŸããerrorlevel controls how extraction errors are handled, see
the corresponding attribute.åŒæ° encoding ããã³ errors ã«ã¯ã¢ãŒã«ã€ãã®èªã¿æžãããšã©ãŒæååã®å€æã«äœ¿çšããæåãšã³ã³ãŒãã£ã³ã°ãæå®ããŸããã»ãšãã©ã®ãŠãŒã¶ãŒã¯ããã©ã«ãèšå®ã®ãŸãŸã§åäœããŸãã詳现ã«é¢ããŠã¯ Unicode ã«é¢ããåé¡ ç¯ãåç §ããŠãã ããã
åŒæ° pax_headers ã¯ããªãã·ã§ã³ã®æååèŸæžã§ãformat ã
PAX_FORMATã ã£ãå Žåã« pax ã°ããŒãã«ããããŒã«è¿œå ãããŸããIf stream is set to
Truethen while reading the archive info about files in the archive are not cached, saving memory.ããŒãžã§ã³ 3.2 ã§å€æŽ: åŒæ° errors ã®ããã©ã«ãã
'surrogateescape'ã«ãªããŸãããããŒãžã§ã³ 3.5 ã§å€æŽ:
'x'(æä»çäœæ) ã¢ãŒãã远å ãããŸãããããŒãžã§ã³ 3.6 ã§å€æŽ: name ãã©ã¡ã¿ã path-like object ãåãä»ããããã«ãªããŸããã
ããŒãžã§ã³ 3.13 ã§å€æŽ: Add the stream parameter.
- classmethod TarFile.open(...)¶
代æ¿ã³ã³ã¹ãã©ã¯ã¿ãŒã§ããã¢ãžã¥ãŒã«ã¬ãã«ã§ã®
tarfile.open()颿°ã¯ãå®éã¯ãã®ã¯ã©ã¹ã¡ãœãããžã®ã·ã§ãŒãã«ããã§ãã
- TarFile.getmember(name)¶
ã¡ã³ã㌠name ã«å¯Ÿãã
TarInfoãªããžã§ã¯ããè¿ããŸããname ãã¢ãŒã«ã€ãã«èŠã€ãããªããã°ãKeyErrorãéåºãããŸããæ³šé
ã¢ãŒã«ã€ãå ã«ã¡ã³ããŒãè€æ°ããå Žåã¯ãæåŸã«åºçŸãããã®ãææ°ã®ããŒãžã§ã³ãšã¿ãªãããŸãã
- TarFile.getmembers()¶
TarInfoã¢ãŒã«ã€ãã®ã¡ã³ããŒããªããžã§ã¯ãã®ãªã¹ããšããŠè¿ããŸãããã®ãªã¹ãã¯ã¢ãŒã«ã€ãå ã®ã¡ã³ããŒãšåãé çªã§ãã
- TarFile.getnames()¶
ã¡ã³ããŒããã®ååã®ãªã¹ããè¿ããŸããããã¯
getmembers()ã§è¿ããããªã¹ããšåãé çªã§ãã
- TarFile.list(verbose=True, *, members=None)¶
å 容ã®äžèЧã
sys.stdoutã«åºåããŸããverbose ãFalseã®å Žåãã¡ã³ããŒåã®ã¿è¡šç€ºããŸããTrueã®å Žåã ls -l ã«äŒŒãåºåãçæããŸãããªãã·ã§ã³ã® members ãäžããå Žåãgetmembers()ãè¿ããªã¹ãã®ãµãã»ããã§ããå¿ èŠããããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: members åŒæ°ã远å ãããŸããã.
- TarFile.next()¶
TarFileãèªã¿èŸŒã¿çšã«ãªãŒãã³ãããŠããæãã¢ãŒã«ã€ãã®æ¬¡ã®ã¡ã³ããŒãTarInfoãªããžã§ã¯ããšããŠè¿ããŸãããããã以äžå©çšå¯èœãªãã®ããªããã°ãNoneãè¿ããŸãã
- TarFile.extractall(path='.', members=None, *, numeric_owner=False, filter=None)¶
ãã¹ãŠã®ã¡ã³ããŒãã¢ãŒã«ã€ãããçŸåšã®äœæ¥ãã£ã¬ã¯ããªãŸã㯠path ã«æœåºããŸãããªãã·ã§ã³ã® members ãäžãããããšãã«ã¯ã
getmembers()ã§è¿ããããªã¹ãã®äžéšã§ãªããã°ãªããŸãããææè ã倿޿å»ãã¢ã¯ã»ã¹æš©éã®ãããªãã£ã¬ã¯ããªæ å ±ã¯ãã¹ãŠã®ã¡ã³ããŒãæœåºãããåŸã«ã»ãããããŸããããã¯äºã€ã®åé¡ãåé¿ããããã§ããäžã€ã¯ãã£ã¬ã¯ããªã®å€æŽæå»ã¯ãã®äžã«ãã¡ã€ã«ãäœæããããã³ã«ãªã»ããããããšããããšãããäžã€ã¯ãã£ã¬ã¯ããªã«æžã蟌ã¿èš±å¯ããªããã°ãã®äžã®ãã¡ã€ã«æœåºã¯å€±æããŠããŸããšããããšã§ããnumeric_owner ã
Trueã®å Žåãtarfile ã® uid ãš gid æ°å€ãæœåºããããã¡ã€ã«ã®ãªãŒããŒ/ã°ã«ãŒããèšå®ããããã«äœ¿çšãããŸããFalse ã®å Žåãtarfile ã®ååä»ãã®å€ã䜿çšãããŸããThe filter argument specifies how
membersare modified or rejected before extraction. See Extraction filters for details. It is recommended to set this explicitly only if specific tar features are required, or asfilter='data'to support Python versions with a less secure default (3.13 and lower).èŠå
Never extract archives from untrusted sources without prior inspection.
Since Python 3.14, the default (
data) will prevent the most dangerous security issues. However, it will not prevent all unintended or insecure behavior. Read the Extraction filters section for details.ããŒãžã§ã³ 3.5 ã§å€æŽ: numeric_owner åŒæ°ã远å ãããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ: path ãã©ã¡ã¿ã path-like object ãåãä»ããããã«ãªããŸããã
ããŒãžã§ã³ 3.12 ã§å€æŽ: filter ãã©ã¡ãŒã¿ã远å ãããŸããã
ããŒãžã§ã³ 3.14 ã§å€æŽ: The filter parameter now defaults to
'data'.
- TarFile.extract(member, path='', set_attrs=True, *, numeric_owner=False, filter=None)¶
ã¢ãŒã«ã€ãããã¡ã³ããŒã®å®å šãªååã䜿ã£ãŠãçŸåšã®ãã£ã¬ã¯ããªã«å±éããŸãããã¡ã€ã«æ å ±ã¯ã§ããéãæ£ç¢ºã«å±éãããŸãã member ã¯ãã¡ã€ã«åãããã¯
TarInfoãªããžã§ã¯ãã§ãã path ã䜿ã£ãŠå¥ã®ãã£ã¬ã¯ããªãæå®ããããšãã§ããŸãã path ã¯ãpath-like object ã§ãæ§ããŸãããset_attrs ã false ã§ãªãéãããã¡ã€ã«ã®å±æ§ (ææè ãæçµæŽæ°æå»ãã¢ãŒã) ã¯èšå®ãããŸããThe numeric_owner and filter arguments are the same as for
extractall().泚é
extract()ã¡ãœããã¯ããã€ãã®å±éã«é¢ããåé¡ãæ±ããŸãããã»ãšãã©ã®å Žåãextractall()ã¡ãœããã®å©çšãèæ ®ããã¹ãã§ããèŠå
Never extract archives from untrusted sources without prior inspection. See the warning for
extractall()for details.ããŒãžã§ã³ 3.2 ã§å€æŽ: ãã©ã¡ãŒã¿ãŒã« set_attrs ã远å ããŸããã
ããŒãžã§ã³ 3.5 ã§å€æŽ: numeric_owner åŒæ°ã远å ãããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ: path ãã©ã¡ã¿ã path-like object ãåãä»ããããã«ãªããŸããã
ããŒãžã§ã³ 3.12 ã§å€æŽ: filter ãã©ã¡ãŒã¿ã远å ãããŸããã
- TarFile.extractfile(member)¶
ã¢ãŒã«ã€ãããã¡ã³ããŒããã¡ã€ã«ãªããžã§ã¯ããšããŠæœåºããŸãã member ã¯ãã¡ã€ã«åã§ã
TarInfoãªããžã§ã¯ãã§ãæ§ããŸãããmember ãäžè¬ãã¡ã€ã«ãŸãã¯ãªã³ã¯ã®å Žåãio.BufferedReaderãªããžã§ã¯ããè¿ãããŸãã ååšãããã以å€ã®ã¡ã³ããŒã§ã¯ãNoneãè¿ãããŸãã ãã以å€ã®å ŽåãNoneãè¿ãããŸãã ã¢ãŒã«ã€ãã« member ãååšããªãå Žåã¯KeyErrorãéåºãããŸããããŒãžã§ã³ 3.3 ã§å€æŽ: æ»ãå€ã
io.BufferedReaderãªããžã§ã¯ãã«ãªããŸãããããŒãžã§ã³ 3.13 ã§å€æŽ: The returned
io.BufferedReaderobject has themodeattribute which is always equal to'rb'.
- TarFile.errorlevel: int¶
If errorlevel is
0, errors are ignored when usingTarFile.extract()andTarFile.extractall(). Nevertheless, they appear as error messages in the debug output when debug is greater than 0. If1(the default), all fatal errors are raised asOSErrororFilterErrorexceptions. If2, all non-fatal errors are raised asTarErrorexceptions as well.Some exceptions, e.g. ones caused by wrong argument types or data corruption, are always raised.
Custom extraction filters should raise
FilterErrorfor fatal errors andExtractErrorfor non-fatal ones.Note that when an exception is raised, the archive may be partially extracted. It is the userâs responsibility to clean up.
- TarFile.extraction_filter¶
Added in version 3.12.
The extraction filter used as a default for the filter argument of
extract()andextractall().The attribute may be
Noneor a callable. String names are not allowed for this attribute, unlike the filter argument toextract().If
extraction_filterisNone(the default), extraction methods will use thedatafilter by default.The attribute may be set on instances or overridden in subclasses. It also is possible to set it on the
TarFileclass itself to set a global default, although, since it affects all uses of tarfile, it is best practice to only do so in top-level applications orsite configuration. To set a global default this way, a filter function needs to be wrapped in@staticmethodto prevent injection of aselfargument.ããŒãžã§ã³ 3.14 ã§å€æŽ: The default filter is set to
data, which disallows some dangerous features such as links to absolute paths or paths outside of the destination. Previously, the default was equivalent tofully_trusted.
- TarFile.add(name, arcname=None, recursive=True, *, filter=None)¶
ãã¡ã€ã« name ãã¢ãŒã«ã€ãã«è¿œå ããŸãã name ã¯ãä»»æã®ãã¡ã€ã«ã¿ã€ã (ãã£ã¬ã¯ããªãfifoãã·ã³ããªãã¯ãªã³ã¯ç)ã§ãã arcname ãäžããããŠããå Žåã¯ãããã¯ã¢ãŒã«ã€ãå ã®ãã¡ã€ã«ã®ä»£æ¿åãæå®ããŸãã ããã©ã«ãã§ã¯ãã£ã¬ã¯ããªã¯ååž°çã«è¿œå ãããŸãã ããã¯ã recursive ã
Falseã«èšå®ãããšé¿ããããŸãã ååž°åŠçã¯ãœãŒããããé åºã§ãšã³ããªãŒã远å ããŸãã filter ãäžããããå Žåãããã¯TarInfoãªããžã§ã¯ããåŒæ°ãšããŠåãåããæäœããTarInfoãªããžã§ã¯ããè¿ã颿°ã§ãªããã°ãªããŸããã 代ããã«Noneãè¿ããå ŽåãTarInfoãªããžã§ã¯ãã¯ã¢ãŒã«ã€ãããé€å€ãããŸãã 䜿çšäŸ ã«ããäŸãåç §ããŠãã ãããããŒãžã§ã³ 3.2 ã§å€æŽ: filter ãã©ã¡ãŒã¿ã远å ãããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ååž°åŠçã¯ãœãŒããããé åºã§ãšã³ããªãŒã远å ããããã«ãªããŸããã
- TarFile.addfile(tarinfo, fileobj=None)¶
Add the
TarInfoobject tarinfo to the archive. If tarinfo represents a non zero-size regular file, the fileobj argument should be a binary file, andtarinfo.sizebytes are read from it and added to the archive. You can createTarInfoobjects directly, or by usinggettarinfo().ããŒãžã§ã³ 3.13 ã§å€æŽ: fileobj must be given for non-zero-sized regular files.
- TarFile.gettarinfo(name=None, arcname=None, fileobj=None)¶
os.stat()ã®çµæããæ¢åã®ãã¡ã€ã«ã«çžåœãããã®ãããTarInfoãªããžã§ã¯ããäœæããŸãããã®ãã¡ã€ã«ã¯ãname ã§åä»ãããããããã¡ã€ã«èšè¿°åãæã€ file object fileobj ãšããŠæå®ãããŸããname 㯠path-like object ã§ãæ§ããŸããã arcname ãäžããããå Žåãã¢ãŒã«ã€ãå ã®ãã¡ã€ã«ã«å¯ŸããŠä»£æ¿åãæå®ããŸããäžããããªãå Žåãåå㯠fileobj ã®name屿§ name 屿§ããåãããŸããååã¯ããã¹ãæååã«ããŠãã ãããTarInfoã®å±æ§ã®äžéšã¯ãaddfile()ã䜿çšããŠè¿œå ããåã«ä¿®æ£ã§ããŸãããã¡ã€ã«ãªããžã§ã¯ããããã¡ã€ã«ã®å é ã«ããéåžžã®ãã¡ã€ã«ãªããžã§ã¯ãã§ãªãå Žåãsizeãªã©ã®å±æ§ã¯ä¿®æ£ãå¿ èŠãããããŸãããããã¯ãGzipFileãªã©ã®å±æ§ã«åœãŠã¯ãŸããŸããnameãä¿®æ£ã§ãããããããããã®å Žåãarcname ã¯ãããŒã®æååã«ããããšãã§ããŸããããŒãžã§ã³ 3.6 ã§å€æŽ: name ãã©ã¡ã¿ã path-like object ãåãä»ããããã«ãªããŸããã
TarInfo ãªããžã§ã¯ã¶
TarInfo ãªããžã§ã¯ã㯠TarFile ã®äžã€ã®ã¡ã³ããŒã衚ããŸãããã¡ã€ã«ã«å¿
èŠãªãã¹ãŠã®å±æ§ (ãã¡ã€ã«ã¿ã€ãããã¡ã€ã«ãµã€ãºãæå»ãã¢ã¯ã»ã¹æš©éãææè
çã®ãããª) ãä¿åããä»ã«ããã®ã¿ã€ããæ±ºå®ããã®ã«åœ¹ã«ç«ã€ããã€ãã®ã¡ãœãããæäŸããŸããããã«ã¯ãã¡ã€ã«ã®ããŒã¿ãã®ãã®ã¯ å«ãŸããŸãã ã
TarInfo objects are returned by TarFile's methods
getmember(), getmembers() and
gettarinfo().
Modifying the objects returned by getmember() or
getmembers() will affect all subsequent
operations on the archive.
For cases where this is unwanted, you can use copy.copy() or
call the replace() method to create a modified copy in one step.
Several attributes can be set to None to indicate that a piece of metadata
is unused or unknown.
Different TarInfo methods handle None differently:
The
extract()orextractall()methods will ignore the corresponding metadata, leaving it set to a default.addfile()will fail.list()will print a placeholder string.
- classmethod TarInfo.frombuf(buf, encoding, errors)¶
TarInfoãªããžã§ã¯ããæååãããã¡ãŒ buf ããäœæããŠè¿ããŸãããããã¡ãŒãäžæ£ãªå Žå
HeaderErrorãéåºããŸãã
- classmethod TarInfo.fromtarfile(tarfile)¶
TarFileãªããžã§ã¯ãã® tarfile ãããæ¬¡ã®ã¡ã³ããŒãèªã¿èŸŒãã§ããããTarInfoãªããžã§ã¯ããšããŠè¿ããŸãã
- TarInfo.tobuf(format=DEFAULT_FORMAT, encoding=ENCODING, errors='surrogateescape')¶
TarInfoãªããžã§ã¯ãããæååãããã¡ãŒãäœæããŸããåŒæ°ã«ã€ããŠã®æ å ±ã¯ãTarFileã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåç §ããŠãã ãããããŒãžã§ã³ 3.2 ã§å€æŽ: åŒæ° errors ã®ããã©ã«ãã
'surrogateescape'ã«ãªããŸããã
TarInfo ãªããžã§ã¯ãã«ã¯ä»¥äžã®ããŒã¿å±æ§ããããŸã:
- TarInfo.mtime: int | float¶
Time of last modification in seconds since the epoch, as in
os.stat_result.st_mtime.ããŒãžã§ã³ 3.12 ã§å€æŽ: Can be set to
Noneforextract()andextractall(), causing extraction to skip applying this attribute.
- TarInfo.mode: int¶
Permission bits, as for
os.chmod().ããŒãžã§ã³ 3.12 ã§å€æŽ: Can be set to
Noneforextract()andextractall(), causing extraction to skip applying this attribute.
- TarInfo.type¶
ãã¡ã€ã«ã¿ã€ããtype ã¯éåžžã宿°
REGTYPEãAREGTYPEãLNKTYPEãSYMTYPEãDIRTYPEãFIFOTYPEãCONTTYPEãCHRTYPEãBLKTYPEããããã¯GNUTYPE_SPARSEã®ããããã§ããTarInfoãªããžã§ã¯ãã®ã¿ã€ãããã£ãšç°¡åã«è§£æ±ºããã«ã¯ãäžèšã®is*()ã¡ãœããã䜿ã£ãŠäžããã
- TarInfo.linkname: str¶
ãªã³ã¯å ãã¡ã€ã«ã®ååãããã¯ã¿ã€ã
LNKTYPEãšSYMTYPEã®TarInfoãªããžã§ã¯ãã«ã ãååšããŸããFor symbolic links (
SYMTYPE), the linkname is relative to the directory that contains the link. For hard links (LNKTYPE), the linkname is relative to the root of the archive.
- TarInfo.uid: int¶
ãã¡ã€ã«ã¡ã³ããŒãä¿åããå ã®ãŠãŒã¶ãŒã®ãŠãŒã¶ãŒ IDã
ããŒãžã§ã³ 3.12 ã§å€æŽ: Can be set to
Noneforextract()andextractall(), causing extraction to skip applying this attribute.
- TarInfo.gid: int¶
ãã¡ã€ã«ã¡ã³ããŒãä¿åããå ã®ãŠãŒã¶ãŒã®ã°ã«ãŒã IDã
ããŒãžã§ã³ 3.12 ã§å€æŽ: Can be set to
Noneforextract()andextractall(), causing extraction to skip applying this attribute.
- TarInfo.uname: str¶
ãã¡ã€ã«ã¡ã³ããŒãä¿åããå ã®ãŠãŒã¶ãŒã®ãŠãŒã¶ãŒåã
ããŒãžã§ã³ 3.12 ã§å€æŽ: Can be set to
Noneforextract()andextractall(), causing extraction to skip applying this attribute.
- TarInfo.gname: str¶
ãã¡ã€ã«ã¡ã³ããŒãä¿åããå ã®ãŠãŒã¶ãŒã®ã°ã«ãŒãåã
ããŒãžã§ã³ 3.12 ã§å€æŽ: Can be set to
Noneforextract()andextractall(), causing extraction to skip applying this attribute.
- TarInfo.sparse¶
Sparse member information.
- TarInfo.pax_headers: dict¶
pax æ¡åŒµããããŒã«é¢é£ä»ãããããkey-value ãã¢ã®èŸæžã
- TarInfo.replace(name=..., mtime=..., mode=..., linkname=..., uid=..., gid=..., uname=..., gname=..., deep=True)¶
Added in version 3.12.
Return a new copy of the
TarInfoobject with the given attributes changed. For example, to return aTarInfowith the group name set to'staff', use:new_tarinfo = old_tarinfo.replace(gname='staff')
By default, a deep copy is made. If deep is false, the copy is shallow, i.e.
pax_headersand any custom attributes are shared with the originalTarInfoobject.
TarInfo ãªããžã§ã¯ãã¯äŸ¿å©ãªç
§äŒçšã®ã¡ãœãããããã€ãæäŸããŠããŸã:
Extraction filters¶
Added in version 3.12.
The tar format is designed to capture all details of a UNIX-like filesystem,
which makes it very powerful.
Unfortunately, the features make it easy to create tar files that have
unintended -- and possibly malicious -- effects when extracted.
For example, extracting a tar file can overwrite arbitrary files in various
ways (e.g. by using absolute paths, .. path components, or symlinks that
affect later members).
In most cases, the full functionality is not needed. Therefore, tarfile supports extraction filters: a mechanism to limit functionality, and thus mitigate some of the security issues.
èŠå
None of the available filters blocks all dangerous archive features. Never extract archives from untrusted sources without prior inspection. See also Hints for further verification.
åè
- PEP 706
Contains further motivation and rationale behind the design.
The filter argument to TarFile.extract() or extractall()
can be:
the string
'fully_trusted': Honor all metadata as specified in the archive. Should be used if the user trusts the archive completely, or implements their own complex verification.the string
'tar': Honor most tar-specific features (i.e. features of UNIX-like filesystems), but block features that are very likely to be surprising or malicious. Seetar_filter()for details.the string
'data': Ignore or block most features specific to UNIX-like filesystems. Intended for extracting cross-platform data archives. Seedata_filter()for details.None(default): UseTarFile.extraction_filter.If that is also
None(the default), the'data'filter will be used.ããŒãžã§ã³ 3.14 ã§å€æŽ: The default filter is set to
data. Previously, the default was equivalent tofully_trusted.A callable which will be called for each extracted member with a TarInfo describing the member and the destination path to where the archive is extracted (i.e. the same path is used for all members):
filter(member: TarInfo, path: str, /) -> TarInfo | None
The callable is called just before each member is extracted, so it can take the current state of the disk into account. It can:
return a
TarInfoobject which will be used instead of the metadata in the archive, orreturn
None, in which case the member will be skipped, orraise an exception to abort the operation or skip the member, depending on
errorlevel. Note that when extraction is aborted,extractall()may leave the archive partially extracted. It does not attempt to clean up.
Default named filters¶
The pre-defined, named filters are available as functions, so they can be reused in custom filters:
- tarfile.fully_trusted_filter(member, path)¶
Return member unchanged.
This implements the
'fully_trusted'filter.
- tarfile.tar_filter(member, path)¶
Implements the
'tar'filter.Strip leading slashes (
/andos.sep) from filenames.Refuse to extract files with absolute paths (in case the name is absolute even after stripping slashes, e.g.
C:/fooon Windows). This raisesAbsolutePathError.Normalize filenames (
TarInfo.name) that contain..components usingos.path.normpath(). Note that this removes internal..components, which may change the meaning of the name if it traverses symbolic links.Refuse to extract files whose absolute path (after following symlinks) would end up outside the destination. This raises
OutsideDestinationError.Clear high mode bits (setuid, setgid, sticky) and group/other write bits (
S_IWGRP|S_IWOTH).
Return the modified
TarInfomember.ããŒãžã§ã³ 3.14.8 ã§å€æŽ: Filenames containing
..components are now normalized.
- tarfile.data_filter(member, path)¶
Implements the
'data'filter. In addition to whattar_filterdoes:Normalize link targets (
TarInfo.linkname) usingos.path.normpath(). Note that this removes internal..components, which may change the meaning of the link if the path inTarInfo.linknametraverses symbolic links.Refuse to extract links (hard or soft) that link to absolute paths, or ones that link outside the destination.
This raises
AbsoluteLinkErrororLinkOutsideDestinationError.Note that such files are refused even on platforms that do not support symbolic links.
Refuse to extract device files (including pipes). This raises
SpecialFileError.For regular files, including hard links:
For other files (directories), set
modetoNone, so that extraction methods skip applying permission bits.Set user and group info (
uid,gid,uname,gname) toNone, so that extraction methods skip setting it.
Return the modified
TarInfomember.Note that this filter does not block all dangerous archive features. See Hints for further verification for details.
ããŒãžã§ã³ 3.14 ã§å€æŽ: Link targets are now normalized.
Filter errors¶
When a filter refuses to extract a file, it will raise an appropriate exception,
a subclass of FilterError.
This will abort the extraction if TarFile.errorlevel is 1 or more.
With errorlevel=0 the error will be logged and the member will be skipped,
but extraction will continue.
Hints for further verification¶
Even with filter='data', tarfile is not suited for extracting untrusted
files without prior inspection.
Among other issues, the pre-defined filters do not prevent denial-of-service
attacks. Users should do additional checks.
Here is an incomplete list of things to consider:
Extract to a
new temporary directoryto prevent e.g. exploiting pre-existing links, and to make it easier to clean up after a failed extraction.Disallow symbolic links if you do not need the functionality.
When working with untrusted data, use external (e.g. OS-level) limits on disk, memory and CPU usage.
Check filenames against an allow-list of characters (to filter out control characters, confusables, foreign path separators, and so on).
Check that filenames have expected extensions (discouraging files that execute when you âclick on themâ, or extension-less files like Windows special device names).
Limit the number of extracted files, total size of extracted data, filename length (including symlink length), and size of individual files.
Check for files that would be shadowed on case-insensitive filesystems.
Also note that:
Tar files may contain multiple versions of the same file. Later ones are expected to overwrite any earlier ones. This feature is crucial to allow updating tape archives, but can be abused maliciously.
tarfile does not protect against issues with âliveâ data, e.g. an attacker tinkering with the destination (or source) directory while extraction (or archiving) is in progress.
Supporting older Python versions¶
Extraction filters were added to Python 3.12, but may be backported to older
versions as security updates.
To check whether the feature is available, use e.g.
hasattr(tarfile, 'data_filter') rather than checking the Python version.
The following examples show how to support Python versions with and without
the feature.
Note that setting extraction_filter will affect any subsequent operations.
Fully trusted archive:
my_tarfile.extraction_filter = (lambda member, path: member) my_tarfile.extractall()
Use the
'data'filter if available, but revert to Python 3.11 behavior ('fully_trusted') if this feature is not available:my_tarfile.extraction_filter = getattr(tarfile, 'data_filter', (lambda member, path: member)) my_tarfile.extractall()
Use the
'data'filter; fail if it is not available:my_tarfile.extractall(filter=tarfile.data_filter)
ãããã¯:
my_tarfile.extraction_filter = tarfile.data_filter my_tarfile.extractall()
Use the
'data'filter; warn if it is not available:if hasattr(tarfile, 'data_filter'): my_tarfile.extractall(filter='data') else: # remove this when no longer needed warn_the_user('Extracting may be unsafe; consider updating Python') my_tarfile.extractall()
Stateful extraction filter example¶
While tarfile's extraction methods take a simple filter callable, custom filters may be more complex objects with an internal state. It may be useful to write these as context managers, to be used like this:
with StatefulFilter() as filter_func:
tar.extractall(path, filter=filter_func)
Such a filter can be written as, for example:
class StatefulFilter:
def __init__(self):
self.file_count = 0
def __enter__(self):
return self
def __call__(self, member, path):
self.file_count += 1
return member
def __exit__(self, *exc_info):
print(f'{self.file_count} files extracted')
ã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ã€ã¹Â¶
Added in version 3.4.
The tarfile module provides a simple command-line interface to interact
with tar archives.
tar ã¢ãŒã«ã€ããæ°èŠã«äœæãããå Žåã-c ãªãã·ã§ã³ã®åŸã«ãŸãšããããã¡ã€ã«åã®ãªã¹ããæå®ããŠãã ãã:
$ python -m tarfile -c monty.tar spam.txt eggs.txt
ãã£ã¬ã¯ããªãæž¡ãããšãã§ããŸã:
$ python -m tarfile -c monty.tar life-of-brian_1979/
tar ã¢ãŒã«ã€ããã«ã¬ã³ããã£ã¬ã¯ããªã«å±éãããå Žåã-e ãªãã·ã§ã³ã䜿çšããŠãã ãã:
$ python -m tarfile -e monty.tar
ãã£ã¬ã¯ããªåãæž¡ãããšã§ tar ã¢ãŒã«ã€ããå¥ã®ãã£ã¬ã¯ããªã«åãåºãããšãã§ããŸã:
$ python -m tarfile -e monty.tar other-dir/
tar ã¢ãŒã«ã€ãå
ã®ãã¡ã€ã«äžèЧã衚瀺ããã«ã¯ -l ã䜿çšããŠãã ãã:
$ python -m tarfile -l monty.tar
ã³ãã³ãã©ã€ã³ãªãã·ã§ã³Â¶
- -c <tarfile> <source1> ... <sourceN>¶
- --create <tarfile> <source1> ... <sourceN>¶
ãœãŒã¹ãã¡ã€ã«ãã tarfile ãäœæããŸãã
- -e <tarfile> [<output_dir>]¶
- --extract <tarfile> [<output_dir>]¶
output_dir ãæå®ãããŠããªãå Žåãã«ã¬ã³ããã£ã¬ã¯ããªã« tarfile ãå±éããŸãã
- -v, --verbose¶
詳现ãåºåããŸãã
- --filter <filtername>¶
Specifies the filter for
--extract. See Extraction filters for details. Only string names are accepted (that is,fully_trusted,tar, anddata).
䜿çšäŸÂ¶
Reading examples¶
tar ã¢ãŒã«ã€ãããçŸåšã®ãã£ã¬ã¯ããªã«ãã¹ãŠæœåºããæ¹æ³:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall(filter='data')
tar.close()
tar ã¢ãŒã«ã€ãã®äžéšãããªã¹ãã®ä»£ããã«ãžã§ãã¬ãŒã¿ãŒé¢æ°ãå©çšã㊠TarFile.extractall() ã§å±éããæ¹æ³:
import os
import tarfile
def py_files(members):
for tarinfo in members:
if os.path.splitext(tarinfo.name)[1] == ".py":
yield tarinfo
tar = tarfile.open("sample.tar.gz")
tar.extractall(members=py_files(tar))
tar.close()
gzip å§çž® tar ã¢ãŒã«ã€ããäœæããŠã¡ã³ããŒæ å ±ã®ããã€ãã衚瀺ããæ¹æ³:
import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print(tarinfo.name, "is", tarinfo.size, "bytes in size and is ", end="")
if tarinfo.isreg():
print("a regular file.")
elif tarinfo.isdir():
print("a directory.")
else:
print("something else.")
tar.close()
Writing examples¶
éå§çž® tar ã¢ãŒã«ã€ãããã¡ã€ã«åã®ãªã¹ãããäœæããæ¹æ³:
import tarfile
tar = tarfile.open("sample.tar", "w")
for name in ["foo", "bar", "quux"]:
tar.add(name)
tar.close()
with æãå©çšããåãäŸ:
import tarfile
with tarfile.open("sample.tar", "w") as tar:
for name in ["foo", "bar", "quux"]:
tar.add(name)
How to create and write an archive to stdout using
sys.stdout.buffer in the fileobj parameter
in TarFile.add():
import sys
import tarfile
with tarfile.open("sample.tar.gz", "w|gz", fileobj=sys.stdout.buffer) as tar:
for name in ["foo", "bar", "quux"]:
tar.add(name)
TarFile.add() 颿°ã® filter åŒæ°ãå©çšããŠãŠãŒã¶ãŒæ
å ±ããªã»ããããªããã¢ãŒã«ã€ããäœæããæ¹æ³:
import tarfile
def reset(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
tar = tarfile.open("sample.tar.gz", "w:gz")
tar.add("foo", filter=reset)
tar.close()
ãµããŒãããŠãã tar ãã©ãŒããã¶
There are three tar formats that can be created with the tarfile module:
POSIX.1-1988 ustar format (
USTAR_FORMAT). ãã¡ã€ã«åã®é·ãã¯256æåãŸã§ã§ããªã³ã¯åã®é·ãã¯100æåãŸã§ã§ããæå€§ã®ãã¡ã€ã«ãµã€ãºã¯8GiBã§ãããã®ãã©ãŒãããã¯å€ããŠå¶éãå€ãã§ãããåºããµããŒããããŠããŸããThe GNU tar format (
GNU_FORMAT). It supports long filenames and linknames, files bigger than 8 GiB and sparse files. It is the de facto standard on GNU/Linux systems.tarfilefully supports the GNU tar extensions for long names, sparse file support is read-only.The POSIX.1-2001 pax format (
PAX_FORMAT). It is the most flexible format with virtually no limits. It supports long filenames and linknames, large files and stores pathnames in a portable way. Modern tar implementations, including GNU tar, bsdtar/libarchive and star, fully support extended pax features; some old or unmaintained libraries may not, but should treat pax archives as if they were in the universally supported ustar format. It is the current default format for new archives.It extends the existing ustar format with extra headers for information that cannot be stored otherwise. There are two flavours of pax headers: Extended headers only affect the subsequent file header, global headers are valid for the complete archive and affect all following files. All the data in a pax header is encoded in UTF-8 for portability reasons.
ä»ã«ããèªã¿èŸŒã¿ã®ã¿ãµããŒãããŠãã tar ãã©ãŒããããããã€ããããŸã:
ancient V7 ãã©ãŒãããããã㯠Unix 7th Edition ããååšãããæåã® tar ãã©ãŒãããã§ããéåžžã®ãã¡ã€ã«ãšãã£ã¬ã¯ããªã®ã¿ä¿åããŸããåå㯠100 æåãè¶ ããŠã¯ãªããããŠãŒã¶ãŒ/ã°ã«ãŒãåã«é¢ããæ å ±ã¯ä¿åãããŸãããããã€ãã®ã¢ãŒã«ã€ãã¯ããã£ãŒã«ãã ASCII ã§ãªãæåãå«ãå Žåã«ãããããŒã®ãã§ãã¯ãµã ã®èšç®ã誀ããŸãã
SunOS tar æ¡åŒµãã©ãŒããããPOSIX.1-2001 pax ãã©ãŒãããã®äºæµã§ãããäºææ§ããããŸããã
Unicode ã«é¢ããåé¡Â¶
tar ãã©ãŒãããã¯ãããšããšããŒããã©ã€ãã«ãã¡ã€ã«ã·ã¹ãã ã®ããã¯ã¢ããããšãç®çã§èšèšãããŸãããçŸåšãtarã¢ãŒã«ã€ãã¯ãã¡ã€ã«ãé åžããéã«äžè¬çã«çšãããããããã¯ãŒã¯äžã§äº€æãããŠããŸãããªãªãžãã«ãã©ãŒããããæ±ããäžã€ã®åé¡ã¯ (ä»ã®å€ãã®ãã©ãŒãããã§ãåãã§ãã)ãæ§ã ãªæåãšã³ã³ãŒãã£ã³ã°ã®ãµããŒãã«ã€ããŠèæ ®ããŠããªãããšã§ããäŸãã°ãUTF-8 ã·ã¹ãã äžã§äœæãããéåžžã® tar ã¢ãŒã«ã€ãã¯ãé ASCII æåãå«ãã§ããå ŽåãLatin-1 ã·ã¹ãã ã§ã¯æ£ããèªã¿åãããšãã§ããŸãããããã¹ãã®ã¡ã¿ããŒã¿ (ãã¡ã€ã«åããªã³ã¯åããŠãŒã¶ãŒ/ã°ã«ãŒãåãªã©) ã¯ç Žå£ãããŸããæ®å¿µãªããšã«ãã¢ãŒã«ã€ãã®ãšã³ã³ãŒãã£ã³ã°ãèªåæ€åºããæ¹æ³ã¯ãããŸãããpax ãã©ãŒãããã¯ãã®åé¡ã解決ããããã«èšèšãããŸãããããã¯é ASCII ã¡ã¿ããŒã¿ããŠãããŒãµã«æåãšã³ã³ãŒãã£ã³ã° UTF-8 ã䜿çšããŠæ ŒçŽããŸãã
The details of character conversion in tarfile are controlled by the
encoding and errors keyword arguments of the TarFile class.
encoding ã¯ã¢ãŒã«ã€ãã®ã¡ã¿ããŒã¿ã«äœ¿çšããæåãšã³ã³ãŒãã£ã³ã°ãæå®ããŸããããã©ã«ãå€ã¯ sys.getfilesystemencoding() ã§ããã©ãŒã«ããã¯ãšã㊠'ascii' ã䜿çšãããŸããã¢ãŒã«ã€ãã®èªã¿æžãæã«ãã¡ã¿ããŒã¿ã¯ãããããã³ãŒããŸãã¯ãšã³ã³ãŒãããªããã°ãªããŸãããencoding ã«é©åãªå€ãèšå®ãããŠããªãå Žåããã®å€æã¯å€±æããããšããããŸãã
åŒæ° errors ã¯æåã倿ã§ããªãæã®æ±ããæå®ããŸããæå®ã§ããå€ã¯ ãšã©ãŒãã³ãã© ç¯ãåç
§ããŠãã ãããããã©ã«ãã®ã¹ããŒã 㯠'surrogateescape' ã§ãPython ã¯ãã®ãã¡ã€ã«ã·ã¹ãã ã®åŒã³åºãã䜿çšããŸãããã¡ã€ã«åãã³ãã³ãã©ã€ã³åŒæ°ãããã³ç°å¢å€æ° ãåç
§ããŠãã ããã
ããã©ã«ãã® PAX_FORMAT ã¢ãŒã«ã€ãã§ã¯ãã¡ã¿ããŒã¿ã¯ãã¹ãŠ UTF-8 ã§æ ŒçŽããããããencoding ã¯éåžžæå®ããå¿
èŠã¯ãããŸãããencoding ã¯ããŸãã«ããããã€ããªã® pax ããããŒããã³ãŒããããå Žåããããã¯ãµãã²ãŒãæåãå«ãæååãæ ŒçŽãããŠããå Žåã«äœ¿çšãããŸãã