Before handling zip archives with Python, you will need to use the appropriate library:
import zipfile
Extraction of zips is straight forward:
zipArchive = "/path/to/file.zip"
extractPath = "/path/to/extraction/directory/"
with zipfile.ZipFile(zipArchive, 'r') as zip_file:
zip_file.extractall(extractPath)
We can also check for the existance of particular files in the archive without having to extract the whole archive:
zipArchive = "/path/to/file.zip"
fileToCheck = "path/relative/to/zip/archive.file"
with zipfile.ZipFile(zipArchive, 'r') as zip_file:
archive_files = zip_file.namelist()
if fileToCheck in archive_files:
print(f"{fileToCheck} exists in the archive")
else:
print(f"{fileToCheck} does not exist in the archive")