from zipfile import ZipFile
import os
Extract zip file
with ZipFile("myzipfile.zip", 'r') as zip_object:
if "text1.txt" in zip_object.namelist():
zip_object.extract("text1.txt", path=Path("C:/my_folder/temp/")) #Extracting a specific file in the zip to a specific location (trailing slash on path is optional)
#zip_object.extractall(path=Path("C:/my_folder/temp")) #If you don't provide a path is will extract to the current folder
#WARNING - if the zip is provided externally are you sure there won't be an attack vector created using extractall() by files you're not expecting being included in the zip or files marked with a specific extract path not being picked up and blocked by the extractall() function?
zip_object.close()
Create zip file
with ZipFile(LIGHTING_IMPORT_FILE_NAME, 'w') as zip_object:
# Adding files that need to be zipped
zip_object.write("text1.txt")
zip_object.write("spreadsheet1.csv")
# Check to see if the zip file is created
if os.path.exists(LIGHTING_IMPORT_FILE_NAME):
print("New lighting file create with changes: " + LIGHTING_IMPORT_FILE_NAME)
else:
print("An error occured, couldn't create new zip file")
