Python

Python ZipInfo Object

Lossless data compression is supported by the archive file format known as ZIP. Lossless compression is when the original data can be precisely recreated from the compressed data, thanks to the compression algorithm. Consequently, a ZIP file is a single file that contains one or more compressed files, providing a perfect approach to reducing the size of huge files and keeping related information together.

We will discover in this post how to use Python’s zipfile zipinfo object to manipulate zip files. Python has an integrated module called zipfile which contains the object zipinfo. Using the class zipfile ZipInfo, one may represent a Zip folder member. When using the getinfo() and infolist() functions on ZipFile objects, objects of the ZipInfo class are returned. An individual ZIP archive member’s information is stored in each object. A ZipInfo object can’t be created directly. Using the additional methods shown below, we may acquire ZipInfo class instances.

  1. A list of ZipInfo objects representing each member of the ZipFile class is kept in the filelist constant of the object.
  2. In addition, a list of ZipInfo class objects for a ZIP file’s members is returned by the infolist() method of a ZipFile class object.
  3. An object of the ZipInfo class belonging to a specific member is returned by the getinfo() function of a ZipFile class object.

Syntax of the ZipInfo object in Python

The following ZipInfo syntax, we deployed in our Python script.

ZipInfo.from_file(filename, arcname=None, strict_timestamps=True)

Below, we have briefly discussed the syntax of ZipInfo.

One obligatory argument is required by the from file() function. The path to the file from which the ZipInfo object was constructed is kept in the “filename” field. This path could be an object of the Path type or a filename of the str class type. Additionally, it has an “arcname” optional argument. The name that is used to create a ZipInfo object is stored in this argument.

Similar to the filename parameter, it has the same default value. Arcname gets the filename in the ZipInfo object when a different name is given to the arcname option. A ZipInfo object is returned by this method. All the information necessary for the file with the specified filename to become a ZIP file is contained in this ZipInfo object. Afterward, this data is retrieved following the user’s needs.

Example 1:
We have utilized the dir() function to study each object in the zipfile ZipInfo class. Look at the code below.

Import zipfile
Print(dir(zipfile.zipInfo))

As already discussed above, ZipInfo can be accessed by the zipfile module in Python. Here, we have added the zipfile module to get the zipInfo object. Then, we have a print method where we have invoked the dir() method to display all the objects the zipInfo class contains. For this, we have passed the zipfile along with the object zipInfo to the print method.

When the above code is debugged, it generates the following outcomes:

Example 2:
We have used the zipInfo instances in the following code to explain the functionality of each method on the zip files. Let’s see the code, we have implemented below.

import zipfile

zf = zipfile.ZipFile("MyFile.zip", "w")
zf.write("MyFile.txt", "dir/MyFile.txt")
print (zf.infolist()[0].filename)

def main():
    with zipfile.ZipFile('MyFile.zip') as file:

        archive = file.infolist()
        read_file = archive[-1]
        print("File name:- {}".format(read_file.filename))

        print("File size:- {}".format(read_file.file_size))

        print("Is directory:- {}".format(read_file.is_dir()))

        print("File date & time:- {}".format(read_file.date_time))

if __name__ == '__main__': main()

Here, we have first imported the zipfile package. Then, we implemented a code to add the file directory to store the zip files. For this, we have created an object as zf and specified the file name inside the zipfile class function ZipFile. We have set the zip file name inside that function in a write mode. We have called the write function, where we set the directory of the file. After that, within the print function, we have called the infolist function for containing all the files and folders of the specified directory.

As the zip file directory is set, we can utilize the ZipInfo object of the class zipfile. We have defined the main function of this program with the def keyword. Inside the function, we have called the zip file which is present as ‘MyFile.zip’. We have created a variable as an archive where we have infolist(). The infolist() is the instance of the class zipfile. The function “infolist()” returns a list of all the files and directories in the zip -> “ZipInfo” objects.

After that, the final item of the list is assigned to a variable ‘archive’ so that all “ZipInfo’s” methods may be tested. In the end, we have a print statement where we have used the zipInfo class objects. The first print statement has the zipinfo.filename which returns the name of the file. Then, we have a zipInfo file size, is_dir, and date_time objects of the zipInfo class to return the specific output according to their functionality.

The ZipInfo objects provided the following output which includes the file name, its directory, the file size, and the date and time of the file.

Example 3:
The ZipInfo class offers numerous methods. These numerous techniques are used to alter a ZIP file and can be used to conduct various operations on it. The from file() method is what we’re talking about here.

from zipfile import ZipFile, ZipInfo

zf = ZipFile("MyFile.zip", mode='r')

print("Name of the existing files in ZIP file : ", zf.namelist(), sep="\n")
print("\nAdding the file into the archieve.")

zip_file = ZipInfo.from_file(filename="MyFile.txt")

print(type(zip_file))
print("Filename of ZipInfo object :", zip_file.filename)

In the program, from the zipfile module, we have imported the zipfile and zipInfo. After that, we specified the file name in write mode inside the variable ‘zf’. Then, we get the list of files placed in the zip file folder by invoking the namelist() method in the print method. We have created another variable as zip_file where we have utilized the zipInfo object from_file and given a file name. Here, we have also obtained the type of the zip file. The last print statement is utilizing the instance filename of the zipInfo.

The implementation of the aforementioned program yields the output listed below.

Conclusion:

The class ZipInfo belongs to the zipfile module. Information about an archive member is represented by this class. The user can get information about the components of a ZIP file using several different techniques that are included in the tool. For you to better understand, we have included several samples.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content