rarfile module in Python
In the following tutorial, we will discuss the rarfile module of the Python programming language. We will understand the different classes of the rarfile module along with some examples.
So, let's get started.
Understanding the Python rarfile module
The rarfile module in Python is used to read the RAR archive. The interface is built as zipfile-like as possible.
The basic functionalities of the rarfile module:
Now, before we start working with the module, let us install it.
How to install the rarfile module in Python?
In order to install the rarfile module, we will be using the pip installer following the command shown below:
In order to verify the module is installed properly, we can create a new file and add the import statement to see if it returns any errors or not.
Now, save the Python file and run the execution command using the command prompt:
If the above Python file does not raise any import error, we are good to go and head onto the Facebook messenger bot building procedure. However, if it does raise an exception, it is recommended to reinstall the module and refer to its official documentation.
Now, let us understand the basics of the rarfile module.
Classes of the rarfile module
The rarfile module provides multiple classes that we can use as per the requirements. These classes are:
We will discuss these classes in brief.
Understanding the RarFile class
The RarFile class of the rarfile module is used to parse the RAR structure, providing access to the files in the archive.
The syntax of the execution of the RarFile class is shown below:
Some of the methods and attributes of the RarFile class is shown below:
1. comment= None
This attribute is used to state the archive comment. The value can either be a Unicode string or None.
2. filename= None
This attribute is used the provide the name of the file, if available. The value can either be a Unicode string or None.
This method is used to open context.
4. __exit__(type, value, traceback)
This method is used to exit context.
This method is used to iterate over members.
This method is used to set the password used during extraction.
This method returns True in case any archive entries need a password for extraction.
This method returns a list containing the names of the file in the archive.
This method returns the RarInfo objects for all files/directories in the archive.
This method returns the filenames of the archive volumes.
If the archive is of only a single volume, the list consists of the name of the main archive file.
This method returns RarInfo for file.
12. open(name, mode = 'r', pwd = None)
This method returns the file-like object (RarExtFile) from where the data can be read.
The object implements io.RawIOBase interface, so we can further wrap it with io.BufferReader and io.TextIOWrapper.
In the previous versions of Python, where the io module is not available, it implements only read(), seek(), tell(), and close() methods.
The object is seek-able, although the seeking is quick only on uncompressed files. On compressed files, the seeking is implemented by reading ahead and restarting the decompression.
13. read(name, pwd = None)
This method returns the uncompressed data for archive entry.
It is recommended to use the open() method for larger files.
This method is used to release open resources.
15. printdir(file = None)
This method is used to print a list of the files in the archive to stdout or a given file.
16. extract(member, path = None, pwd = None)
This method is used to extract a single file into the current directory.
17. extractall(path = None, members = None, pwd = None)
This method is used to extract all files into the current directory.
18. testrar(pwd = None)
This method is used to read all files and test CRC.
This method returns an error string if parsing fails or None if no exception has occurred.
Understanding the RarInfo class
The RarInfo class of the rarfile module is used as an entry in the RAR archive.
Timestamps as datetime are without time zone in RAR3, with UTC zone in RAR5 archives
The syntax of the execution of the RarInfo class is shown below:
Some of the methods and attributes of the RarInfo class is shown below:
This attribute contains the name of the file with a relative path. The value of this attribute is always a Unicode string specifying the path separated by a Path separator as '/'.
This attribute consists of the timestamp of File modification. It can be used as a tuple of (year, month, day, hour, minute, second). RAR5 allows archives where it is missing, and it's None then.
This attribute includes the optional file comment field. The value consists of a Unicode string. (RAR3-only)
This attribute is used to specify the uncompressed size.
This attribute is used to specify the compressed size.
This attribute is used to specify the method of the compression: One of the RAR_M0, …, RAR_M5 constants.
This attribute consists of the minimal version of RAR that is required for decompression. As (major*10 + minor), so 2.9 is 29.
RAR3: 10, 20, 29
RAR5 does not have such a field in the archive, and it is set to 50.
This attribute specifies the Host OS type, one of RAR_OS_* constants.
RAR3: RAR_OS_WIN32, RAR_OS_UNIX, RAR_OS_MSDOS, RAR_OS_OS2, RAR_OS_BEOS
RAR5: RAR_OS_WIN32, RAR_OS_UNIX
This attribute is used to specify the file attributes. It may be either dos-style or unix-style, depending on host_os.
This attribute is used to specify the time of the file modification. The value can be the same as the date_time attribute; however, as a datetime object with extended precision.
This attribute is an optional time field specifying the time of creation. It also acts as a datetime object.
This attribute is also an optional time field specifying the time of last access. It also acts as a datetime object.
This attribute is also an optional time field specifying the archival time. It also acts as a datetime object. (RAR3-only)
This attribute is used to specify the CRC-32 of the uncompressed file. The value of this attribute is an unsigned int.
RAR5: may be None.
This attribute is used to specify the Blake2SP hash over decompressed data. (RAR5-only)
This attribute is used to specify the volume nr, beginning from 0.
This attribute is used to specify the volume file name where the file begins.
This attribute consists of a tuple of (type, flags, target). (RAR5-only). If not None, the file is the link of some sort. (RAR5-only)
Type is one of the constants:
Flags may contain bits:
This method is used to return True if the entry is a directory.
New in version 4.0.
This method is used to return True if the entry is a symlink.
New in version 4.0.
This method is used to return True if the entry is a normal file.
New in version 4.0.
This method is used to return True if data is stored password protected.
This method is used to return True if the entry is a directory.
Deprecated since version 4.0.
Understanding the RarExtFile class
The RarExtFile class of the rarfile module works as a base class for objects similar to the file that RarFile.open() returns.
The syntax for the RarExtFile class is shown below:
This class provides public methods and common CRC checking
Some attributes and methods of the RarExtFile class are shown below:
1. name= None
This attribute is used to specify the file name of the archive entry
This method is used to read all or a specified amount of data from archive entry.
This method is used to close open resources.
This method is used to define zero-copy read directly into the buffer. It Returns bytes read.
This method returns the current reading position in uncompressed data.
6. seek(offset, whence = 0)
This method is used to seek data. On uncompressed files, the seeking works by actual seek, so it is fast. On compressed files, it is slow - forward seeking happens by reading ahead, backward by re-opening and decompressing from the start.
This method returns True
This method returns False as writing is not supported.
This method returns True as seeking is supported, although it's slow on compressed files.
This method is used to read all the remaining data
This method returns the underlying file descriptor if one exists. OSError is raised if the IO object does not utilize a file descriptor.
This method returns if this is an 'interactive' stream. It also returns False if it can't be determined.
This method is used to read and return a line from the stream. The line terminator is always b'n' for binary files; for text files, we can use the newlines argument to open in order to select the line terminator(s) recognized. If the size is given, size bytes will be read at most.
This method is used to return a list of lines from the stream. We can specify the hint to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds the hint.
Understanding the nsdatetime class
The nsdatetime class of the rarfile module represents the Datetime that carries nanoseconds. This class does not support Arithmetic and will lose nanoseconds.
New in version 4.0
The syntax of the nsdatetime class is shown below:
Some attributes and methods of the nsdatetime class are shown below:
This attribute consists of the number of nanoseconds ranging from 0 to 999999999.
2. isoformat(sep = 'T', timespec = 'auto')
This method is used to format with nanosecond precision by default.
This method is used to convert to new time zone.
4. replace(year = None, month = None, day = None, hour = None, minute = None, second = None, microsecond = None, tzinfo = None, *, fold = None, nanosecond = None)
This method is used to return new timestamp with given fields replaced.
Functions of the rarfile module
Some of the functions of the rarfile module are as follows:
Constants of the rarfile module
Some of the constants of the rarfile module are as follows
Warnings and Exceptions of the rarfile module
Some of the Warnings and Exceptions of the rarfile module are as follows:
Working of the rarfile module
Let us now consider the following example demonstrating the working of the rarfile module.
myfolder/helloWorld.py 101 myfolder/image.jpg 281466 myfolder/readme.txt 49 b'Hello Python learners!\r\nWelcome to Javatpoint.com' myfolder/ 0
In the above snippet of code, we have imported the required module. We have then used the RarFile class to select the RAR archive. We have then used the for-loop to iterate through the files present in the archive and print the filenames along with their sizes. We have then used the if conditional statement to check if the RAR archive contains a readme.txt file. At last, we have read the file using the read() method.