Sling Academy
Home/Python/How to Zip/Unzip a Folder using Python

How to Zip/Unzip a Folder using Python

Last updated: January 07, 2024

Introduction

Managing file archives is a common task in software development. Python, with its rich standard library and third-party packages, offers robust tools for zipping and unzipping folders efficiently. This tutorial walks through the essentials and dives into some advanced techniques for handling zip files in Python.

Getting Started with zipfile

Python’s built-in zipfile module is the cornerstone for archive management. To use it, you’ll first need to import the module:

import zipfile

Create a new ZIP file by instantiating the ZipFile class:

with zipfile.ZipFile('example.zip', 'w') as zipf:
    zipf.write('myfile.txt')

To zip an entire folder, you can write a function that traverses directories and adds files correspondingly:

import os

def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))

with zipfile.ZipFile('example.zip', 'w') as zipf:
    zipdir('myfolder', zipf)

Now, let’s switch gears to unzipping. You can extract all contents to a directory using the extractall method:

with zipfile.ZipFile('example.zip', 'r') as zip_ref:
    zip_ref.extractall('target_directory')

Working with Password-Protected ZIP Files

Password-protecting a ZIP file adds a layer of security. The following example shows how to create and extract password-protected archives:

# Creating a password-protected ZIP file
with zipfile.ZipFile('example_secure.zip', 'w') as zipf:
    zipf.setpassword(b'my_password')
    zipf.write('secret_file.txt')

# Extracting from a password-protected ZIP file
with zipfile.ZipFile('example_secure.zip', 'r') as zipf:
    zipf.setpassword(b'my_password')
    zipf.extractall('extracted_folder')

Advanced Archive Options: Compression Types

Choosing the right compression type can affect both the size of the archive and the speed of compression and decompression. Python’s zipfile module supports various compression methods, including ZIP_STORED and ZIP_DEFLATED:

# Using different compression types
with zipfile.ZipFile('example_deflated.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
    zipf.write('large_file.txt')

with zipfile.ZipFile('example_stored.zip', 'w', zipfile.ZIP_STORED) as zipf:
    zipf.write('large_file.txt')

Handling Large ZIP Files

When working with large files, it’s critical to manage resources wisely. You can create or unzip large files incrementally:

# Writing large files to a zip one chunk at a time
chunk_size = 4096
with open('Huge_file.txt', 'rb') as f_in:
    with zipfile.ZipFile('large_file.zip', 'w') as zipf:
        for chunk in iter(lambda: f_in.read(chunk_size), b''):
            zipf.writestr('stored_filename.txt', chunk)

# Extracting large files from a zip one chunk at a time
with zipfile.ZipFile('large_file.zip', 'r') as zipf:
    with zipf.open('stored_filename.txt') as zf:
        with open('extracted_file.txt', 'wb') as f_out:
            for chunk in iter(lambda: zf.read(chunk_size), b''):
                f_out.write(chunk)

Working with Third-Party Libraries

Beyond Python’s standard library, packages like PyZip and zipstream offer additional functionalities. To use these, you would typically install them using pip:

pip install pyzip
pip install zipstream

After installation, you can explore more advanced features such as zipping/unzipping with different algorithms, handling split archives, and processing zip files without extracting them to disk.

Conclusion

Whether you’re working on a simple script or an enterprise application, Python’s tools for zipping and unzipping folders are both powerful and versatile. This tutorial has provided a glimpse into the myriad of possibilities available within Python’s ecosystem for handling zip files. Experimentation and practice will make these techniques second nature in your Python programming endeavors.

Next Article: 3 Ways to Resize Images in Python

Previous Article: How to Get OS Information in Python

Series: Python: System & File I/O Tutorials

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types