pythonzipunzip

How to unzip specific folder from a .zip with Python


I am looking to unzip a particular folder from a .zip in Python:

e.g. archive.zip contains the folders foo and bar, I want to unzip foo to a specific location, retaining it's folder structure.


Solution

  • Check zipfile module.

    For your case:

    import zipfile
    
    archive = zipfile.ZipFile('archive.zip')
    
    for file in archive.namelist():
        if file.startswith('foo/'):
            archive.extract(file, 'destination_path')