rarfile — Work with RAR archives¶
The RAR file format is a common archive and compression standard. This module provides tools to read and list a RAR file.
This module is based on the UnRAR library (provided by RARLAB) through a ctypes wrapper, and inspired on Python’s ZipFile.
The module defines the following items:
- exception rarfile.BadRarFile¶
The error raised for bad RAR files.
- class rarfile.RarFile¶
The class for reading RAR files. See section RarFile Objects for constructor details.
- class rarfile.RarInfo¶
Class used to represent information about a member of an archive. Instances of this class are returned by the getinfo() and infolist() methods of RarFile objects. Most users of the rarfile module will not need to create these, but only use those created by this module. header should be a RARHeaderDataEx instance as returned by unrarlib; the fields are described in section RarInfo Objects.
Returns True if filename is a valid RAR file based on its magic number, otherwise returns False.
- Official RAR site.
- RAR addons
- RAR addons where you can download UnRAR library sources.
- class rarfile.RarFile(file[, mode='r'])
Open a RAR file, where file should be a path to a file (a string). The mode parameter should be 'r' to read an existing file (only allowed mode at the moment).
Return a RarInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError.
Return a list containing a RarInfo object for each member of the archive. The objects are in the same order as their entries in the actual RAR file on disk if an existing archive was opened.
Return a list of archive members by name.
- RarFile.open(member[, pwd])¶
Extract a member from the archive as a file-like object (see Python’s io.BytesIO). member is the name of the file in the archive, or a RarInfo object. pwd is the password used for encrypted files.
New in version 0.3.
- RarFile.read(name[, pwd])¶
Return the bytes of the file member in the archive. member is the name of the file in the archive, or a RarInfo object. pwd is the password used for encrypted files and, if specified, it will override the default password set with setpassword().
New in version 0.3.
- RarFile.extract(member, path=None, pwd=None)¶
Extract a member from the archive to the current working directory; member must be its full name or a RarInfo object). Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or a RarInfo object. pwd is the password used for encrypted files.
- RarFile.extractall(path=None, members=None, pwd=None)¶
Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). pwd is the password used for encrypted files.
Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/" or filenames with two dots "..".
Print a table of contents for the archive to sys.stdout.
Set pwd as default password to extract encrypted files.
Read all the files in the archive and check their CRC’s and file headers. Return the name of the first bad file, or else return None.
The following data attribute is also available:
The comment text associated with the RAR file, if any.
Instances of the RarInfo class are returned by the getinfo() and infolist() methods of RarFile objects. Each object stores information about a single member of the RAR archive.
Instances have the following attributes:
Name of the file in the archive.
The time and date of the last modification to the archive member. This is a tuple of six values:
Index Value 0 Year (>= 1980) 1 Month (one-based) 2 Day of month (one-based) 3 Hours (zero-based) 4 Minutes (zero-based) 5 Seconds (zero-based)
The RAR file format does not support timestamps before 1980.
Type of compression for the archive member.
Comment for the individual archive member.
System which created RAR archive.
RAR version needed to extract archive.
RAR flag bits.
CRC-32 of the uncompressed file.
Size of the compressed data.
Size of the uncompressed file.