zipfile to compress and uncompress ZIP files in Python

Money and Business

The zipfile module of the Python standard library can be used to compress files into ZIPs and uncompress ZIP files. It is included in the standard library, so no additional installation is required.

The following contents are explained.

  • Compress multiple files into a ZIP file
  • Add a new file to an existing ZIP file
  • Compress a directory (folder) into a ZIP file
  • Compressed into a ZIP file with a password
  • Check the contents of the ZIP file.
  • Extract (unpack) the entire contents of the ZIP file.
  • Select the contents of the ZIP file and extract it.

Compress multiple files into a ZIP file

Create a ZipFile object and use the write() method to add the files you want to compress.

To create a new ZIP file, specify the path of the ZIP file to be created as the first argument of the constructor of the ZipFile object, and the second argument as follows'w'

In addition, the compression method can be specified as the third argument.

  • zipfile.ZIP_STORED:Just combine multiple files without compression (default)
  • zipfile.ZIP_DEFLATED:Normal ZIP compression (zlib module required)
  • zipfile.ZIP_BZIP2:BZIP2 compression (bz2 module required)
  • zipfile.ZIP_LZMA:LZMA compression (lzma module required)

BZIP2 and LZMA have a higher compression ratio (can be compressed to a smaller size), but the time required for compression is longer.

In the write() method, the file with the first argument filename is written to a ZIP file with the second argument arcname. If arcname is omitted, filename is used as is. arcname can also specify a directory structure.

The ZipFile object needs to be closed with the close() method, but if you use the with statement, it will be closed automatically when the block is finished.

import zipfile

with zipfile.ZipFile('data/temp/new_comp.zip', 'w', compression=zipfile.ZIP_DEFLATED) as new_zip:
    new_zip.write('data/temp/test1.txt', arcname='test1.txt')
    new_zip.write('data/temp/test2.txt', arcname='zipdir/test2.txt')
    new_zip.write('data/temp/test3.txt', arcname='zipdir/sub_dir/test3.txt')

By specifying the compress_type argument of the write() method, it is also possible to select the compression method for each file.

with zipfile.ZipFile('data/temp/new_comp_single.zip', 'w') as new_zip:
    new_zip.write('data/temp/test1.txt', arcname='test1.txt', compress_type=zipfile.ZIP_DEFLATED)
    new_zip.write('data/temp/test2.txt', arcname='zipdir/test2.txt')
    new_zip.write('data/temp/test3.txt', arcname='zipdir/sub_dir/test3.txt')

Add a new file to an existing ZIP file

To add a new file to an existing zip file, set the first argument of the constructor to the path of the existing zip file when creating the ZipFile object. Also, set the second argument mode as follows.'a'

Then, as in the example above, just add the file using the write() method.

with zipfile.ZipFile('data/temp/new_comp.zip', 'a') as existing_zip:
    existing_zip.write('data/temp/test4.txt', arcname='test4.txt')

Compress a directory (folder) into a ZIP file

If you want to compress a whole directory (folder) into a single ZIP file, you can use os.scandir() or os.listdir() to make a list of files, but it is easier to use make_archive() in the shutil module.

See the following article.

Compressed into a ZIP file with a password

The zipfile module does not allow you to create password-protected ZIPs. If you want to compress a file into a password protected zip file, use the third party library pyminizip.

Note that the decompression of password-protected ZIPs can be done with the zipfile module (see below).

Check the contents of the ZIP file.

You can check the contents of an existing ZIP file.

Create a ZipFile object by setting the first argument file in the constructor to the path of the existing zip file and the second argument mode to 'r'. The mode argument can be omitted since the default is 'r'.

You can use the namelist() method of the ZipFile object to get a list of archived files.

with zipfile.ZipFile('data/temp/new_comp.zip') as existing_zip:
    print(existing_zip.namelist())
# ['test1.txt', 'zipdir/test2.txt', 'zipdir/sub_dir/test3.txt', 'test4.txt']

Extract (unpack) the entire contents of the ZIP file.

To unpack the contents of a ZIP file, create a ZipFile object with the first argument file in the constructor as the path to the existing ZIP file and the second argument mode as 'r', as in the example above. The mode argument can be omitted since it defaults to 'r'.

The extractall() method of the ZipFile object extracts (uncompresses) the entire contents of the ZIP file. The first argument, path, specifies the path of the directory to extract to. If it is omitted, the files will be extracted to the current directory.

with zipfile.ZipFile('data/temp/new_comp.zip') as existing_zip:
    existing_zip.extractall('data/temp/ext')

A ZIP file with a password can be extracted by specifying the password as the argument pwd of the extractall() method.

with zipfile.ZipFile('data/temp/new_comp_with_pass.zip') as pass_zip:
    pass_zip.extractall('data/temp/ext_pass', pwd='password')

Select the contents of the ZIP file and extract it.

If you want to unpack and extract only certain files, use the extract() method.

The first argument of the extract() method is the name of the file to extract, and the second argument path is the path of the directory to extract to. If the path argument is omitted, the file will be extracted to the current directory. The name of the file to be extracted should include the path to the directory in the ZIP file if it is stored there.

with zipfile.ZipFile('data/temp/new_comp.zip') as existing_zip:
    existing_zip.extract('test1.txt', 'data/temp/ext2')

Like the extractall() method, the extract() method also allows you to specify a password as the argument pwd.

with zipfile.ZipFile('data/temp/new_comp_with_pass.zip') as pass_zip:
    pass_zip.extract('test1.txt', 'data/temp/ext_pass2', pwd='password')