Zipping Files With Python

In this tutorial, we will learn about Zipping Files With Python. Let’s start!!

Zipping Files With Python

The zipfile module in Python provides a way to work with ZIP files. It allows you to create, extract, and manipulate ZIP files in a convenient and easy-to-use manner. With the zipfile module, you can:

  • Create new ZIP files and add files to them
  • Extract files from existing ZIP files
  • Print the contents of a ZIP file
  • Read the contents of a file within a ZIP file
  • Set and read the password of a ZIP file
  • And other related functionalities.

The zipfile module provides a ZipFile class that can be used to work with ZIP files. The ZipFile class provides methods like write(), extract(), extractall(), printdir(), read(), namelist(), infolist(), and close() to perform various operations on ZIP files.

To create a new ZIP file, you can use the ZipFile class in write mode, ‘w’, and add files to it using the write() method. To extract files from a ZIP file, you can use the extractall() method. Also, To print the contents of a ZIP file, you can use the printdir() method.

zipfile modes in python

The zipfile module in Python provides several modes for opening and interacting with ZIP files. These modes are used when creating an instance of the ZipFile class. The following are the available modes in the zipfile module:

1. ‘r’: You can read an existing ZIP file using this mode. Since it is opened in read-only mode, no changes may be made.

2. ‘w’: This mode is used to write a new ZIP file or to overwrite an existing file. Any existing contents in the file will be destroyed when the file is opened in write mode.

3. ‘a’: You can append to an existing ZIP file using this mode. New files can be added to the existing ZIP file when the file is opened in write mode.

4. ‘x’: Only fresh ZIP files are created using this option.

If the file already exists, the operation will raise a “FileExistsError”

Here’s an example of how to use these modes:

# reading an existing zip file
with zipfile.ZipFile('existing_file.zip', 'r') as zip:
    zip.printdir()
    
# creating a new zip file
with zipfile.ZipFile('new_file.zip', 'w') as zip:
    zip.write('file1.txt')
    
# appending to an existing zip file
with zipfile.ZipFile('existing_file.zip', 'a') as zip:
    zip.write('file2.txt')
    
# creating a new zip file exclusively
with zipfile.ZipFile('exclusive_file.zip', 'x') as zip:
    zip.write('file1.txt')

It’s crucial to remember that closing the file when you’re finished working with it is always a good practise when working with ZIP files. This can be achieved by using the with statement as shown in the examples above.

zipfile methods in python

1. ZipFile.write(filename, arcname=None, compress_type=None):

This method is used to add a file to the ZIP archive. The parameters filename and arcname specify the names of the files to be added and the archive, respectively. The compress_type parameter is used to specify the compression method to be used.

Example:

    
import zipfile

# create a new zip file
with zipfile.ZipFile('my_files.zip', 'w') as zip:
    # add a file to the archive
    zip.write('file1.txt')
    # add a file to the archive with a different name in the archive
    zip.write('file2.txt', 'my_file2.txt')
    # add a file to the archive with a different compression method
    zip.write('file3.txt', compress_type=zipfile.ZIP_DEFLATED)

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

This method is used to extract a single file from the archive. The member parameter is the name of the file to be extracted, and the path parameter is the directory where the file should be extracted. The pwd parameter is used to specify a password for the archive.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # extract a single file
    zip.extract('file1.txt')
    # extract a single file to a specific directory
    zip.extract('file2.txt', 'extracted_files')
    # extract a single file with password
    zip.extract('file3.txt', pwd=b'secret')

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

This method is used to extract all the files from the archive. The path parameter is the directory where the files should be extracted, the members parameter is a list of files that should be extracted, and the pwd parameter is used to specify a password for the archive.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # extract all files
    zip.extractall()
    # extract all files to a specific directory
    zip.extractall(path='extracted_files')
    # extract specific files
    zip.extractall(members=['file1.txt', 'file2.txt'])
    # extract all files with password
    zip.extractall(pwd=b'secret')

4. ZipFile.printdir():

This method is used to print the contents of the archive. It shows the name, size, and date of the files in the archive.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # print the contents of the archive
    zip.printdir()

Output:

File Name                                             Modified                   Size

file1.txt                                      2020-11-22 12:30:20            12

file2.txt                                      2020-11-22 12:31:01            23

5. ZipFile.read(name, pwd=None):

This approach is used to read a file’s contents from an archive.. The name parameter is the name of the file to be read, and the pwd parameter is used to specify a password for the archive.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # read the contents of a file
    content = zip.read('file1.txt')
    print(content)
    # read the contents of a file with password
    content = zip.read('file2.txt', pwd=b'secret')
    print(content)

6. ZipFile.namelist():

This method returns a list of all the files in the archive.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # get a list of the names of the files in the archive
    files = zip.namelist()
    print(files)

7. ZipFile.infolist():

This function provides a list of ZipInfo objects, which include details like the file’s name, size, and date about each item in the archive.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # get a list of `ZipInfo` objects
    files_info = zip.infolist()
    for file in files_info:
        print(file.filename)
        print(file.file_size)
        print(file.date_time)

8. ZipFile.close():

When you’re through with an archive, you can close it using this technique.

Example:

import zipfile

# name of zip file
zip_name = 'my_files.zip'

# open the zip file
with zipfile.ZipFile(zip_name, 'r') as zip:
    # perform operations on the archive
    zip.extractall()
    # close the archive
    zip.close()

Conclusion

In summary, the zipfile module in Python provides a powerful and easy-to-use interface for working with ZIP files. It allows you to perform a wide range of operations on ZIP files, making it a valuable tool for any Python developer.