In this article we will discuss different ways to get detail about all files in a zip archive like file’s name & size etc.

Get the name of all files in the ZIP archive using ZipFile.namelist()

In Python’s zipfile module, ZipFile class provides a member function to get the names of all files in it i.e.

ZipFile.namelist()

It returns a list of file names in Zip archive.

Let’s use this to get the list of files in a zip file ,

First of all, import the module,

from zipfile import ZipFile

Create a ZipFile object by opening the ‘sampleDir.zip’ in read mode and get the list of files in it using ZipFile.namelist() i.e.
# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   # Get list of files names in zip
   listOfiles = zipObj.namelist()
   # Iterate over the list of file names in given list & print them
   for elem in listOfiles:
       print(elem)

Output:
sampleDir/sample_file.csv
sampleDir/logs/test_1.log
sampleDir/logs/test_2.log

As it returns only the names of file in archive. But if we want more details like size, last modification etc of all entries in zip archive?

Let’s see how to do that,

Get detail info like name, size etc of files in a Zip file using ZipFile.infolist()

In Python’s zipfile module ZipFile class provides an another member function to the get the details of each entry in zipfile i.e.

ZipFile.infolist()

It returns a list of ZipInfo objects for each member of the archive.

Each ZipInfo object for a file in archive contains many information about the file like name, last modification time , permission & size etc.

Let’s use this to to iterate over all the files in zip & print details like name, size, compressed size & last modification datetime.

# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   # Get list of ZipInfo objects
   listOfiles = zipObj.infolist()
   # Iterate of over the list of ZipInfo objects & access members of the object
   for elem in listOfiles:
       print(elem.filename, ' : ', elem.file_size, ' : ', elem.date_time, ' : ', elem.compress_size)

Output:
sampleDir/sample_file.csv  :  2829  :  (2018, 11, 30, 21, 44, 46)  :  2829
sampleDir/logs/test_1.log  :  3386  :  (2018, 11, 30, 21, 44, 36)  :  3386
sampleDir/logs/test_2.log  :  3552  :  (2018, 11, 30, 21, 44, 56)  :  3552

Print the details of ZIP archive to std.out using ZipFile.printdir()

In Python’s zipfile module ZipFile class provides another member function to print the contents of zip file as table i.e.

ZipFile.printdir()

It will print the details of files in ZIP archive, as tabular format in std.out .
Let’s see an example,
# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   zipObj.printdir()

Output:
File Name                                             Modified             Size
sampleDir/sample_file.csv                      2018-11-30 21:44:46         2829
sampleDir/logs/test_1.log                      2018-11-30 21:44:36         3386
sampleDir/logs/test_2.log                      2018-11-30 21:44:56         3552

Complete example is as follows,
from zipfile import ZipFile


def main():

    print('*** Get the list of file names in zip file ***')
    # Create a ZipFile Object and load sample.zip in it
    with ZipFile('sampleDir.zip', 'r') as zipObj:
       # Get list of files names in zip
       listOfiles = zipObj.namelist()
       # Iterate over the list of file names in given list & print them
       for elem in listOfiles:
           print(elem)


    print('*** Get the detail info of files in zip file like name, size & last modification time ***')

    # Create a ZipFile Object and load sample.zip in it
    with ZipFile('sampleDir.zip', 'r') as zipObj:
       # Get list of ZipInfo objects
       listOfiles = zipObj.infolist()
       # Iterate of over the list of ZipInfo objects & access members of the object
       for elem in listOfiles:
           print(elem.filename, ' : ', elem.file_size, ' : ', elem.date_time, ' : ', elem.compress_size)


    print('*** Print the details of all files in ZIP File ***')

    # Create a ZipFile Object and load sample.zip in it
    with ZipFile('sampleDir.zip', 'r') as zipObj:
       zipObj.printdir()


if __name__ == '__main__':
   main()

Output:
*** Get the list of file names in zip file ***
sampleDir/sample_file.csv
sampleDir/logs/test_1.log
sampleDir/logs/test_2.log
*** Get the detail info of files in zip file like name, size & last modification time ***
sampleDir/sample_file.csv  :  2829  :  (2018, 11, 30, 21, 44, 46)  :  2829
sampleDir/logs/test_1.log  :  3386  :  (2018, 11, 30, 21, 44, 36)  :  3386
sampleDir/logs/test_2.log  :  3552  :  (2018, 11, 30, 21, 44, 56)  :  3552
*** Print the details of all files in ZIP File ***
File Name                                             Modified             Size
sampleDir/sample_file.csv                      2018-11-30 21:44:46         2829
sampleDir/logs/test_1.log                      2018-11-30 21:44:36         3386
sampleDir/logs/test_2.log                      2018-11-30 21:44:56         3552