Python: How to unzip a file | Extract Single, multiple or all files from a ZIP archive

In this article we will discuss different ways to unzip or extract single, multiple or all files from zip archive to current or different directory.

In Python’s zipfile module, ZipFile class provides a member function to extract all the contents from a ZIP archive,

ZipFile.extractall(path=None, members=None, pwd=None)

It accepts following arguments :

  • path : location where zip file need to be extracted, if not provided it will extract the contents in current directory.
  • members : list of files to be extracted. It will extract all the files in zip if this argument is not provided.
  • pwd : If zip file is encrypted then pass password in this argument default is None.

Module required :

from zipfile import ZipFile

Let’s use this to extract all the contents from zip files.

Extract all files from a zip file to current directory

Suppose we have a zip file ‘sample.zip’. in our current directory, let’s see how to extract all files from it.
To unzip it first create a ZipFile object by opening the zip file in read mode and then call extractall() on that object i.e.

# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   # Extract all the contents of zip file in current directory
   zipObj.extractall()

It will extract all the files in zip at current Directory.  If files with same name are already present at extraction location then it will overwrite those files.

Extract all files from a zip file to different directory

To extract all the files from zip file to a different directory, we can pass the destination location as argument in extractall(). Path can be relative or absolute.

# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   # Extract all the contents of zip file in different directory
   zipObj.extractall('temp')

It will extract all the files in ‘sample.zip’ in  temp folder.

Extract few files from a large zip file based on condition

Suppose we have a very large zip file and we need a few files from thousand of files in the archive. Unzipping all files from large zip can take minutes. But if are interested in few of the archived files only, then instead of unzipping the whole file we can extract a single file too from the zip file.

In Python’s zipfile module, ZipFile class provides a member function to extract a single from a ZIP File,

ZipFile.extract(member, path=None, pwd=None)

It accepts following arguments :

  • member : Full name of file to be extracted. It should one from the list of archived files names returned by ZipFile.namelist()
  • path : location where zip file need to be extracted, if not provided it will extract the file in current directory.
  • pwd : If zip file is encrypted then pass password in this argument default is None.

Let’s use this to extract only csv files from a zip file i.e.

# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   # Get a list of all archived file names from the zip
   listOfFileNames = zipObj.namelist()
   # Iterate over the file names
   for fileName in listOfFileNames:
       # Check filename endswith csv
       if fileName.endswith('.csv'):
           # Extract a single file from zip
           zipObj.extract(fileName, 'temp_csv')

It will extract only csv files from given zip archive.

Complete example is as follows,

from zipfile import ZipFile


def main():

    print('Extract all files in ZIP to current directory')
    # Create a ZipFile Object and load sample.zip in it
    with ZipFile('sampleDir.zip', 'r') as zipObj:
       # Extract all the contents of zip file in current directory
       zipObj.extractall()

    print('Extract all files in ZIP to different directory')

    # Create a ZipFile Object and load sample.zip in it
    with ZipFile('sampleDir.zip', 'r') as zipObj:
       # Extract all the contents of zip file in different directory
       zipObj.extractall('temp')

    print('Extract single file from ZIP')

    # Create a ZipFile Object and load sample.zip in it
    with ZipFile('sampleDir.zip', 'r') as zipObj:
       # Get a list of all archived file names from the zip
       listOfFileNames = zipObj.namelist()
       # Iterate over the file names
       for fileName in listOfFileNames:
           # Check filename endswith csv
           if fileName.endswith('.csv'):
               # Extract a single file from zip
               zipObj.extract(fileName, 'temp_csv')



if __name__ == '__main__':
   main()

 

 

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top