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")
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through resources like this. We hope you find it helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support here. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *