import zipfile with zipfile.ZipFile('example.zip', 'r') as zip_file: file_list = zip_file.infolist() # print file name and size of each file in the zip for file in file_list: print(file.filename, file.file_size)
import zipfile with zipfile.ZipFile('example.zip', 'w') as zip_file: # add files to the zip zip_file.write("file1.txt") zip_file.write("file2.txt") # print the list of files in the zip file_list = zip_file.infolist() for file in file_list: print(file.filename)In this example, we create a new zip file named `example.zip` in write mode using `ZipFile()` method. We add two text files to the zip using the `write()` method. We then call `infolist()` to get the list of files in the zip. Finally, we print the name of each file in the zip. The package library for `ZipFile` class and `infolist()` method is `zipfile` module which is a built-in module in Python.