2012年6月10日 星期日

[Python Std Library] Data Compress/Archive : tarfile — Read and write tar archive files


來源自 這裡
Preface :
This module makes it possible to read and write tar archives, including those using gzip or bz2 compression. Use the zipfile module to read or write .zip files, or the higher-level functions in shutil. Some facts and figures :
* Reads and writes gzip and bz2 compressed archives.
* Read/write support for the POSIX.1-1988 (ustar) format.
* Read/write support for the GNU tar format including longname and longlink extensions, read-only support for the sparse extension.
* Read/write support for the POSIX.1-2001 (pax) format.

New in version 2.6.
* Handles directories, regular files, hardlinks, symbolic links, fifos, character devices and block devices and is able to acquire and restore file information like timestamp, access permissions and owner.

Supported APIs : 
You can use below API to return TarFile object :
tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
Return a TarFile object for the pathname namemode is a string of the form 'filemode[:compression]' (defaults to 'r'). Here is a full list of mode combinations :


Note that 'a:gz' or 'a:bz2' is not possible. If mode is not suitable to open a certain (compressed) file for reading, ReadError is raised. Use mode 'r' to avoid this. If a compression method is not supported, CompressionError is raised.

If fileobj is specified, it is used as an alternative to a file object opened for name. It is supposed to be at position 0.

For special purposes, there is a second format for mode: 'filemode|[compression]'. tarfile.open() will return a TarFile object 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 a read() or write() method (depending on the mode). bufsize specifies the blocksize and defaults to 20 * 512 bytes. Use this variant in combination with e.g. sys.stdin, a socket file object or a tape device. However, such a TarFile object is limited in that it does not allow to be accessed randomly, see Examples. The currently possible modes :

- class tarfile.TarFile
Class for reading and writing tar archives. Do not use this class directly, better use tarfile.open() instead. See TarFile Objects.

tarfile.is_tarfile(name)
Return True if name is a tar archive file, that the tarfile module can read.

- class tarfile.TarFileCompat(filename, mode='r', compression=TAR_PLAIN)
Deprecated since version 2.6: The TarFileCompat class has been removed in Python 3.
Class for limited access to tar archives with a zipfile-like interface. Please consult the documentation of the zipfile module for more details. compression must be one of the following constants :
TAR_PLAIN : Constant for an uncompressed tar archive.
TAR_GZIPPED : Constant for a gzip compressed tar archive.

- exception tarfile.TarError
Base class for all tarfile exceptions.

- exception tarfile.ReadError
Is raised when a tar archive is opened, that either cannot be handled by the tarfile module or is somehow invalid.

- exception tarfile.CompressionError
Is raised when a compression method is not supported or when the data cannot be decoded properly.

- exception tarfile.StreamError
Is raised for the limitations that are typical for stream-like TarFile objects.

- exception tarfile.ExtractError
Is raised for non-fatal errors when using TarFile.extract(), but only if TarFile.errorlevel== 2.

- exception tarfile.HeaderError
New in version 2.6.
Is raised by TarInfo.frombuf() if the buffer it gets is invalid.


Each of the following constants defines a tar archive format that the tarfile module is able to create. See section Supported tar formats for details.
tarfile.USTAR_FORMAT
POSIX.1-1988 (ustar) format.

tarfile.GNU_FORMAT
GNU tar format.

tarfile.PAX_FORMAT
POSIX.1-2001 (pax) format.

tarfile.DEFAULT_FORMAT
The default format for creating archives. This is currently GNU_FORMAT.

The following variables are available on module level :
tarfile.ENCODING
The default character encoding i.e. the value from either sys.getfilesystemencoding() or sys.getdefaultencoding().

TarFile Objects :
New in version 2.7: Added support for the context manager protocol.
The TarFile object provides an interface to a tar archive. A tar archive is a sequence of blocks. An archive member (a stored file) is made up of a header block followed by data blocks. It is possible to store a file in a tar archive several times. Each archive member is represented by a TarInfo object, see TarInfo Objects for details.

TarFile object can be used as a context manager in a with statement. It will automatically be closed when the block is completed. Please note that in the event of an exception an archive opened for writing will not be finalized; only the internally used file object will be closed. See the Examples section for a use case.

- class tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
All following arguments are optional and can be accessed as instance attributes as well.

name is the pathname of the archive. It can be omitted if fileobj is given. In this case, the file object’s name attribute is used if it exists.

mode is either 'r' to read from an existing archive, 'a' to append data to an existing file or 'w' to create a new file overwriting an existing one.

If fileobj is given, it is used for reading or writing data. If it can be determined, mode is overridden by fileobj‘s mode. fileobj will be used from position 0.
Note. fileobj is not closed, when TarFile is closed.

New in version 2.6.
format controls the archive format. It must be one of the constants USTAR_FORMATGNU_FORMAT or PAX_FORMAT that are defined at module level.

If dereference is False, add symbolic and hard links to the archive. If it is True, add the content of the target files to the archive. This has no effect on systems that do not support symbolic links.

If ignore_zeros is False, treat an empty block as the end of the archive. If it is True, skip empty (and invalid) blocks and try to get as many members as possible. This is only useful for reading concatenated or damaged archives.

debug can be set from 0 (no debug messages) up to 3 (all debug messages). The messages are written to sys.stderr.

If errorlevel is 0, all errors are ignored when using TarFile.extract(). Nevertheless, they appear as error messages in the debug output, when debugging is enabled. If 1, all fatal errors are raised as OSError or IOError exceptions. If 2, all non-fatal errors are raised as TarError exceptions as well.

The encoding and errors arguments control the way strings are converted to unicode objects and vice versa. The default settings will work for most users. See sectionUnicode issues for in-depth information.

The pax_headers argument is an optional dictionary of unicode strings which will be added as a pax global header if format is PAX_FORMAT.

TarFile.open(...)
Alternative constructor. The tarfile.open() function is actually a shortcut to this classmethod.

TarFile.getmember(name)
Return a TarInfo object for member name. If name can not be found in the archive, KeyError is raised.
Note: If a member occurs more than once in the archive, its last occurrence is assumed to be the most up-to-date version.

TarFile.getmembers()
Return the members of the archive as a list of TarInfo objects. The list has the same order as the members in the archive.

TarFile.getnames()
Return the members as a list of their names. It has the same order as the list returned by getmembers().

TarFile.list(verbose=True)
Print a table of contents to sys.stdout. If verbose is False, only the names of the members are printed. If it is True, output similar to that of ls -l is produced.

TarFile.next()
Return the next member of the archive as a TarInfo object, when TarFile is opened for reading. Return None if there is no more available.

TarFile.extractall(path=".", members=None)
New in version 2.5.
Extract all members from the archive to the current working directory or directory path. If optional members is given, it must be a subset of the list returned bygetmembers(). Directory information like owner, modification time and permissions are set after all members have been extracted. This is done to work around two problems: A directory’s modification time is reset each time a file is created in it. And, if a directory’s permissions do not allow writing, extracting files to it will fail.

TarFile.extract(member, path="")
Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. member may be a filename or a TarInfo object. You can specify a different directory using path.

TarFile.extractfile(member)
Extract a member from the archive as a file object. member may be a filename or a TarInfo object. If member is a regular file, a file-like object is returned. If memberis a link, a file-like object is constructed from the link’s target. If member is none of the above, None is returned.

TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
Changed in version 2.6: Added the exclude parameter.
Changed in version 2.7: Added the filter parameter.
Deprecated since version 2.7:
The exclude parameter is deprecated, please use the filter parameter instead. For maximum portability, filter should be used as a keyword argument rather than as a positional argument so that code won’t be affected when exclude is ultimately removed.

Add the file name to the archive. name may be any type of file (directory, fifo, symbolic link, etc.). If given, arcname specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting recursive to False. If exclude is given it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False). If filter is specified it must be a function that takes a TarInfo object argument and returns the changed TarInfo object. If it instead returns None the TarInfo object will be excluded from the archive. See Examples for an example.

TarFile.addfile(tarinfo, fileobj=None)
Add the TarInfo object tarinfo to the archive. If fileobj is given, tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects usinggettarinfo().

TarFile.gettarinfo(name=None, arcname=None, fileobj=None)
Create a TarInfo object for either the file name or the file object fileobj (using os.fstat() on its file descriptor). You can modify some of the TarInfo‘s attributes before you add it using addfile(). If given, arcname specifies an alternative name for the file in the archive.

TarFile.close()
Close the TarFile. In write mode, two finishing zero blocks are appended to the archive.

TarFile.pax_headers
New in version 2.6.
A dictionary containing key-value pairs of pax global headers.

Examples :
How to extract an entire tar archive to the current working directory :


How to extract a subset of a tar archive with TarFile.extractall() using a generator function instead of a list:


How to create an uncompressed tar archive from a list of filenames :


How to read a gzip compressed tar archive and display some member information :


How to create an archive and reset the user information using the filter parameter in TarFile.add() :
  1. import tarfile  
  2. def reset(tarinfo):  
  3.     tarinfo.uid = tarinfo.gid = 0  
  4.     tarinfo.uname = tarinfo.gname = "root"  
  5.     return tarinfo  
  6. tar = tarfile.open("sample.tar.gz""w:gz")  
  7. tar.add("foo", filter=reset)  
  8. tar.close()  
Supplement :
* TarInfo Objects
This object represents one member in a TarFile. Aside from storing all required attributes of a file (like file type, size, time, permissions, owner etc.), it provides some useful methods to determine its type. It does not contain the file’s data itself.

* Supported tar formats
There are three tar formats that can be created with the tarfile module...

* Unicode issues
The tar format was originally conceived to make backups on tape drives with the main focus on preserving file system information. Nowadays tar archives are commonly used for file distribution and exchanging archives over networks. One problem of the original format (that all other formats are merely variants ofis that there is no concept of supporting different character encodings...
This message was edited 42 times. Last update was at 11/06/2012 10:18:37

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...