How to Read the Contents of a Zip File in Python
In this article, we show how to read the contents of a zip file in Python.
A zip file is a file that is composed of compressed files and folders. This helps reduce the overall file size.
Python has a module named the zipmodule module that allows us to work with zip files, including creating zip files, reading zip files, and extracting all files and folders from zip files.
So using Python, we can read the contents of a zip file, including the files and folders that compose it and the sizes of the uncompressed and compressed files.
We can list all contents of a zip file using the namelist() function, the printdir() function, and the filelist property.
We show this in the code below.
So the first thing we need to do is import the zipfile module.
Again, this is the module that allows us to work and do functions on zip files.
We import the os module just to show the current working directory that we are working with, where the zip file is located and where the extracted files will be.
We then create a variable, myzipfile, which is created to hold a zipfile object.
The full line of code is, myzipfile= zipfile.ZipFile('newdocuments.zip')
The namelist() function lists all the files and folders in a zip file.
In this case, there are 4 files in the zip file.
So this is one way of getting the contents of a zip file in Python.
We can also use the printdir() function, which gives us more information than the namelist() function. The printdir() function gives us the name of each file, when each file was last modified, and the size of each file in bytes.
We then use the filelist property to get even more information, including the name of each file, the compression type each file has, the filemode, the size of each uncompressed file, and the size of each compressed file.
We can then access each file by using the getinfo() function. This allows us to get information about an individual file in the zip file.
In this example, we get the information for the 'Application.pdf' file.
We gets its uncompressed file size and its compressed file size.
And this is how we can read the contents of a zip file in Python.
Related Resources
How to Create a Zip File in Python
How to Extract All Files and Folders from a Zip File in Python