Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • 📝 Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • 📝 Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • 📝 Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • 📝 Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • 📝 While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • 📝 For Each Assignments
    • 📝 All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • 📝 Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • 📝 Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • 📝 Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • 📝 Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • 📝 Assignments
  • Exception
    • Error vs Exception
    • Types of Exception
    • Exception Handling
    • User Defined Exception
    • Logging Exception
    • 📝 Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • 📝 Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • 📝 Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • 📝 Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • 📝 Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • 📝 Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • 📝 Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • 📝 Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • 📝 Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • 📝 Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • 📝 Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • 📝 Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • 📝 Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • 📝 Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

Python: Zipping and Unzipping Files

Python Zipping and Unzipping Files - Complete Guide

Learn how to compress and extract files using Python's zipfile module.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers, and data scientists to make programming easy through practical examples, real-world experience, and clear explanations.
On this page:
  • What is File Compression?
  • The zipfile Module
  • Creating ZIP Files
  • Adding Files to a ZIP
  • Extracting ZIP Files
  • Reading ZIP Contents
  • Error Handling
  • Best Practices
  • Try It Yourself
  • Quick Quiz
  • Frequently Asked Questions
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
What You'll Learn Here
  • What is file compression — saving space and organizing files
  • zipfile module — Python's built-in ZIP tools
  • Creating ZIP files — compressing your files
  • Adding to ZIP — updating existing archives
  • Extracting ZIP — unpacking compressed files
  • Reading ZIP contents — exploring archives

What is File Compression?

Have you ever zipped a folder to send it by email? That's file compression in action. It's a way to reduce the size of files so they take up less space and are easier to share. Python's zipfile module lets you do this programmatically.

Think of compression like vacuum-packing clothes. The clothes take up less space and you can unpack them later when you need them. ZIP files work the same way — they pack your files into a smaller package that can be unpacked later.

💡 Key concept: ZIP files are archives that contain one or more files compressed to save space. They're widely used for backups, sharing, and organizing files. The zipfile module gives you full control over this process.

The zipfile Module

1

Python's Built-in ZIP Tools

# Python's zipfile module lets you work with ZIP archives

import zipfile
import os

# Creating sample files
with open("file1.txt", "w") as f:
    f.write("This is file 1")
with open("file2.txt", "w") as f:
    f.write("This is file 2")
with open("file3.txt", "w") as f:
    f.write("This is file 3")

# 1. Check if a file is a ZIP archive
def is_zip_file(filename):
    """Check if a file is a ZIP archive"""
    return zipfile.is_zipfile(filename)

# 2. Creating a simple ZIP file
print("Creating a ZIP file...")
with zipfile.ZipFile("simple.zip", "w") as zipf:
    zipf.write("file1.txt")
    zipf.write("file2.txt")
print("Created: simple.zip")

# 3. Check if it's a ZIP file
print(f"Is simple.zip a ZIP file? {is_zip_file('simple.zip')}")
print(f"Is file1.txt a ZIP file? {is_zip_file('file1.txt')}")

# 4. List contents of a ZIP file
with zipfile.ZipFile("simple.zip", "r") as zipf:
    print(f"Contents of simple.zip:")
    for filename in zipf.namelist():
        print(f"  {filename}")

zipfile module key points:

  • Built-in — no installation needed
  • Read and write — create and extract archives
  • List contents — see what's inside a ZIP
  • Check validity — verify if a file is a ZIP

Quick Check: What module is used to work with ZIP files in Python? (Answer: zipfile)

Creating ZIP Files

2

Compressing Your Files

# Creating ZIP files with the zipfile module

import zipfile
import os

# 1. Create a ZIP file with multiple files
def create_zip(zip_name, file_list):
    """Create a ZIP file from a list of files"""
    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for file in file_list:
            if os.path.exists(file):
                zipf.write(file)
                print(f"  Added: {file}")
            else:
                print(f"  Warning: {file} not found")
    print(f"Created: {zip_name}")

# 2. Create ZIP with compression
def create_compressed_zip(zip_name, file_list, compression=zipfile.ZIP_DEFLATED):
    """Create a ZIP with specified compression"""
    with zipfile.ZipFile(zip_name, 'w', compression) as zipf:
        for file in file_list:
            zipf.write(file)
    print(f"Created compressed ZIP: {zip_name}")

# 3. Create ZIP with a different name inside
def create_zip_with_rename(zip_name):
    """Create a ZIP with renamed files inside"""
    with zipfile.ZipFile(zip_name, 'w') as zipf:
        zipf.write("file1.txt", "renamed1.txt")
        zipf.write("file2.txt", "renamed2.txt")
    print(f"Created ZIP with renamed files: {zip_name}")

# 4. Create ZIP from a directory
def zip_directory(directory, zip_name):
    """Create a ZIP of an entire directory"""
    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for root, dirs, files in os.walk(directory):
            for file in files:
                full_path = os.path.join(root, file)
                arcname = os.path.relpath(full_path, start=os.path.dirname(directory))
                zipf.write(full_path, arcname)
    print(f"Zipped directory: {directory} → {zip_name}")

# Let's create some files to zip
for i in range(5):
    with open(f"doc_{i}.txt", "w") as f:
        f.write(f"Document {i}")

# Create a ZIP
create_zip("documents.zip", ["doc_0.txt", "doc_1.txt", "doc_2.txt"])

# Create a compressed ZIP
create_compressed_zip("compressed.zip", ["doc_3.txt", "doc_4.txt"])

# Create ZIP with renamed files
create_zip_with_rename("renamed.zip")

# Create a directory and zip it
os.makedirs("project", exist_ok=True)
with open("project/data.txt", "w") as f:
    f.write("Project data")
with open("project/notes.txt", "w") as f:
    f.write("Project notes")
zip_directory("project", "project.zip")

Creating ZIP files key points:

  • 'w' mode — write/create a new ZIP
  • ZIP_DEFLATED — compression method
  • Add files — use write() method
  • Rename inside — specify different name in archive
  • Directory support — can zip entire folders

Quick Check: What mode is used to create a new ZIP file? (Answer: 'w')

Adding Files to a ZIP

3

Updating Existing Archives

# Adding files to an existing ZIP archive

import zipfile
import os

# Create a ZIP with one file
with zipfile.ZipFile("update.zip", "w") as zipf:
    zipf.write("doc_0.txt")
print("Created: update.zip")

# 1. Add a file to an existing ZIP
def add_to_zip(zip_name, file_to_add):
    """Add a file to an existing ZIP"""
    with zipfile.ZipFile(zip_name, 'a') as zipf:
        zipf.write(file_to_add)
    print(f"Added {file_to_add} to {zip_name}")

add_to_zip("update.zip", "doc_1.txt")
add_to_zip("update.zip", "doc_2.txt")

# 2. Add multiple files
def add_multiple_to_zip(zip_name, file_list):
    """Add multiple files to a ZIP"""
    with zipfile.ZipFile(zip_name, 'a') as zipf:
        for file in file_list:
            zipf.write(file)
            print(f"  Added: {file}")

add_multiple_to_zip("update.zip", ["doc_3.txt", "doc_4.txt"])

# 3. Check what's in the ZIP
with zipfile.ZipFile("update.zip", "r") as zipf:
    print(f"Contents of update.zip:")
    for filename in zipf.namelist():
        print(f"  {filename}")

# 4. Add a file with a different name
def add_file_with_new_name(zip_name, file, new_name):
    """Add a file with a new name inside the ZIP"""
    with zipfile.ZipFile(zip_name, 'a') as zipf:
        zipf.write(file, new_name)
    print(f"Added {file} as {new_name} in {zip_name}")

add_file_with_new_name("update.zip", "doc_4.txt", "final_doc.txt")

# 5. Add files from a directory
def add_directory_to_zip(zip_name, directory):
    """Add all files from a directory to an existing ZIP"""
    with zipfile.ZipFile(zip_name, 'a') as zipf:
        for root, dirs, files in os.walk(directory):
            for file in files:
                full_path = os.path.join(root, file)
                arcname = os.path.relpath(full_path)
                zipf.write(full_path, arcname)
                print(f"  Added: {arcname}")

# 6. 'a' mode for appending
# Important: You can only add to ZIPs, not remove or modify existing files

Adding to ZIP key points:

  • 'a' mode — append to existing ZIP
  • Add files — same write() method
  • Rename inside — specify new name
  • Add directories — walk and add all files
  • Cannot remove — only add new files

Quick Check: What mode is used to add files to an existing ZIP? (Answer: 'a')

Extracting ZIP Files

4

Unpacking Compressed Files

# Extracting files from a ZIP archive

import zipfile
import os

# 1. Extract all files
def extract_all(zip_name, extract_to="extracted"):
    """Extract all files from a ZIP"""
    os.makedirs(extract_to, exist_ok=True)
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        zipf.extractall(extract_to)
    print(f"Extracted all files from {zip_name} to {extract_to}")

# 2. Extract specific files
def extract_specific(zip_name, files_to_extract, extract_to="."):
    """Extract specific files from a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        for file in files_to_extract:
            zipf.extract(file, extract_to)
            print(f"  Extracted: {file}")

# 3. Extract a single file
def extract_file(zip_name, file_name, extract_to="."):
    """Extract a single file from a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        zipf.extract(file_name, extract_to)
    print(f"Extracted: {file_name} from {zip_name}")

# 4. Extract with progress
def extract_with_progress(zip_name, extract_to="extracted"):
    """Extract with progress indication"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        files = zipf.namelist()
        total = len(files)
        print(f"Extracting {total} files...")
        for i, file in enumerate(files, 1):
            zipf.extract(file, extract_to)
            print(f"  Progress: {i}/{total} - {file}")
    print("Extraction complete!")

# 5. Extract to a specific directory
def extract_to_directory(zip_name, target_dir):
    """Extract ZIP to a specific directory"""
    os.makedirs(target_dir, exist_ok=True)
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        zipf.extractall(target_dir)
    print(f"Extracted to: {target_dir}")

# Let's create a sample ZIP first
with zipfile.ZipFile("sample_extract.zip", "w") as zipf:
    zipf.write("doc_0.txt")
    zipf.write("doc_1.txt")
    zipf.write("doc_2.txt")
    zipf.write("doc_3.txt")

# Test the extraction functions
extract_all("sample_extract.zip")
extract_specific("sample_extract.zip", ["doc_0.txt", "doc_2.txt"])
extract_file("sample_extract.zip", "doc_1.txt")
extract_to_directory("sample_extract.zip", "my_extracted_files")

Extracting key points:

  • extractall() — extract everything
  • extract() — extract specific files
  • 'r' mode — read the ZIP file
  • Target directory — choose where to extract
  • Progress tracking — monitor extraction

Quick Check: What method extracts all files from a ZIP? (Answer: extractall())

Reading ZIP Contents

5

Exploring Archives Without Extracting

# Reading ZIP contents without extracting

import zipfile
import os

# 1. Get list of files
def list_zip_contents(zip_name):
    """List all files in a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        print(f"Contents of {zip_name}:")
        for info in zipf.infolist():
            print(f"  {info.filename} - {info.file_size} bytes")

# 2. Get file information
def get_file_info(zip_name, file_name):
    """Get information about a specific file in a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        info = zipf.getinfo(file_name)
        print(f"File: {info.filename}")
        print(f"  Size: {info.file_size} bytes")
        print(f"  Compressed: {info.compress_size} bytes")
        print(f"  Compression ratio: {info.compress_size / info.file_size * 100:.1f}%")

# 3. Read a file from ZIP without extracting
def read_file_from_zip(zip_name, file_name):
    """Read the content of a file inside a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        content = zipf.read(file_name)
        return content

# 4. Search for files in a ZIP
def search_in_zip(zip_name, pattern):
    """Search for files matching a pattern in a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        matches = []
        for filename in zipf.namelist():
            if pattern in filename:
                matches.append(filename)
        return matches

# 5. Get ZIP file statistics
def get_zip_stats(zip_name):
    """Get statistics about a ZIP file"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        total_files = len(zipf.namelist())
        total_size = sum(info.file_size for info in zipf.infolist())
        total_compressed = sum(info.compress_size for info in zipf.infolist())
        
        print(f"ZIP: {zip_name}")
        print(f"  Total files: {total_files}")
        print(f"  Total size: {total_size} bytes")
        print(f"  Compressed size: {total_compressed} bytes")
        print(f"  Savings: {total_size - total_compressed} bytes")

# 6. Check if a file exists in a ZIP
def file_exists_in_zip(zip_name, file_name):
    """Check if a file exists in a ZIP"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        return file_name in zipf.namelist()

# Create a sample ZIP
with zipfile.ZipFile("read_sample.zip", "w") as zipf:
    zipf.write("doc_0.txt")
    zipf.write("doc_1.txt")
    zipf.write("doc_2.txt")

# Test the functions
list_zip_contents("read_sample.zip")
get_file_info("read_sample.zip", "doc_0.txt")

# Read a file from the ZIP
content = read_file_from_zip("read_sample.zip", "doc_0.txt")
print(f"Content of doc_0.txt: {content.decode()}")

# Search for files
matches = search_in_zip("read_sample.zip", "doc")
print(f"Files containing 'doc': {matches}")

# Get statistics
get_zip_stats("read_sample.zip")

Reading ZIP key points:

  • namelist() — list of file names
  • infolist() — detailed information
  • read() — read file content without extracting
  • getinfo() — get specific file info
  • Statistics — analyze ZIP contents

Quick Check: How do you read a file from a ZIP without extracting it? (Answer: Use the read() method)

Error Handling with ZIP Files

6

Robust ZIP Operations

# Handling errors when working with ZIP files

import zipfile
import os

# 1. Safe ZIP creation
def safe_create_zip(zip_name, file_list):
    """Safely create a ZIP file"""
    try:
        with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
            for file in file_list:
                if os.path.exists(file) and os.path.isfile(file):
                    zipf.write(file)
                else:
                    print(f"Warning: {file} not found or not a file")
        return True
    except PermissionError:
        print(f"Permission denied: Cannot create {zip_name}")
        return False
    except Exception as e:
        print(f"Error creating ZIP: {e}")
        return False

# 2. Safe ZIP extraction
def safe_extract_zip(zip_name, extract_to="extracted"):
    """Safely extract a ZIP file"""
    try:
        if not zipfile.is_zipfile(zip_name):
            print(f"Error: {zip_name} is not a valid ZIP file")
            return False
        
        os.makedirs(extract_to, exist_ok=True)
        with zipfile.ZipFile(zip_name, 'r') as zipf:
            zipf.extractall(extract_to)
        return True
    except PermissionError:
        print(f"Permission denied: Cannot extract to {extract_to}")
        return False
    except zipfile.BadZipFile:
        print(f"Error: {zip_name} is corrupted or not a ZIP file")
        return False
    except Exception as e:
        print(f"Error extracting ZIP: {e}")
        return False

# 3. Safe file addition
def safe_add_to_zip(zip_name, file_to_add):
    """Safely add a file to a ZIP"""
    try:
        if not zipfile.is_zipfile(zip_name):
            print(f"Error: {zip_name} is not a valid ZIP file")
            return False
        
        if not os.path.exists(file_to_add):
            print(f"Error: {file_to_add} does not exist")
            return False
        
        with zipfile.ZipFile(zip_name, 'a') as zipf:
            zipf.write(file_to_add)
        return True
    except Exception as e:
        print(f"Error adding to ZIP: {e}")
        return False

# 4. Safe ZIP info reading
def safe_read_zip_info(zip_name):
    """Safely read ZIP information"""
    try:
        if not zipfile.is_zipfile(zip_name):
            print(f"Error: {zip_name} is not a valid ZIP file")
            return None
        
        with zipfile.ZipFile(zip_name, 'r') as zipf:
            return zipf.namelist()
    except Exception as e:
        print(f"Error reading ZIP: {e}")
        return None

Error handling key points:

  • BadZipFile — corrupted or invalid ZIP
  • PermissionError — can't access files
  • Check validity — use is_zipfile()
  • Check existence — verify files exist
  • Always handle errors — make code robust

Quick Check: What exception is raised for a corrupted ZIP file? (Answer: BadZipFile)

Best Practices for ZIP Files

7

Professional ZIP File Handling

# Best practices for working with ZIP files

import zipfile
import os

# 1. Use context managers
# ✅ Good
with zipfile.ZipFile("archive.zip", "w") as zipf:
    zipf.write("file.txt")

# ❌ Bad
# zipf = zipfile.ZipFile("archive.zip", "w")
# zipf.write("file.txt")
# zipf.close()  # Might be forgotten

# 2. Use compression
# ✅ Good - saves space
with zipfile.ZipFile("compressed.zip", "w", zipfile.ZIP_DEFLATED) as zipf:
    zipf.write("file.txt")

# 3. Check if files exist before adding
def add_file_safely(zipf, file_path, arcname=None):
    """Add a file only if it exists"""
    if os.path.exists(file_path) and os.path.isfile(file_path):
        zipf.write(file_path, arcname)
        return True
    return False

# 4. Use meaningful names inside ZIP
with zipfile.ZipFile("archive.zip", "w") as zipf:
    zipf.write("original_name.txt", "clean_name.txt")

# 5. Handle large files carefully
def create_zip_with_progress(zip_name, file_list):
    """Create a ZIP with progress reporting"""
    total = len(file_list)
    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for i, file_path in enumerate(file_list, 1):
            if os.path.exists(file_path):
                zipf.write(file_path)
                print(f"Added {i}/{total}: {file_path}")
            else:
                print(f"Warning: {file_path} not found")

# 6. Verify ZIP integrity
def verify_zip(zip_name):
    """Verify a ZIP file's integrity"""
    try:
        with zipfile.ZipFile(zip_name, 'r') as zipf:
            # Test reading all files
            for file in zipf.namelist():
                zipf.read(file)
        return True
    except Exception:
        return False

# 7. Extract safely (avoid path traversal)
def safe_extract(zip_name, extract_to="extracted"):
    """Extract safely without path traversal"""
    with zipfile.ZipFile(zip_name, 'r') as zipf:
        for member in zipf.namelist():
            # Prevent path traversal attacks
            if member.startswith('/') or '..' in member:
                print(f"Warning: Skipping potentially unsafe file: {member}")
                continue
            zipf.extract(member, extract_to)

# 8. Clean up temporary files
# Always remove temporary ZIP files when done
# os.remove("temp.zip")

Best practices summary:

  • Use context managers — automatic closing
  • Use compression — save space with ZIP_DEFLATED
  • Check existence — verify files before adding
  • Use meaningful names — organize inside ZIP
  • Verify integrity — test ZIP files
  • Extract safely — prevent path traversal

Quick Check: What compression method should you use? (Answer: ZIP_DEFLATED)

Try It Yourself

Experiment with zipping and unzipping files in the editor below.

Loading Pyodide... 0%
Python Code Editor
========================================
ZIPPING AND UNZIPPING PRACTICE
========================================

1. CREATING SAMPLE FILES
Created: sample1.txt, sample2.txt, sample3.txt

2. CREATING A ZIP FILE
Created: my_archive.zip

3. LISTING ZIP CONTENTS
Files in archive:
sample1.txt
sample2.txt
sample3.txt

4. EXTRACTING ZIP FILE
Extracted to: extracted_files/

5. READING FROM ZIP
sample1.txt content: This is sample file 1

6. ADDING TO EXISTING ZIP
Added sample4.txt to my_archive.zip

Zipping and unzipping practice complete!
🏆

You've Got It!

You now know how to work with ZIP files in Python. You can create, add to, extract, and read ZIP archives.

Quick Quiz

Test what you've learned:

1. What module is used to work with ZIP files in Python?
2. What mode is used to create a new ZIP file?
3. What method extracts all files from a ZIP?
4. What compression method saves the most space?
5. What is the best practice for working with ZIP files?

Frequently Asked Questions

What is the difference between ZIP_STORED and ZIP_DEFLATED? ▼

ZIP_STORED just stores files without compression (fast but takes more space). ZIP_DEFLATED compresses files using the DEFLATE algorithm (slower but saves space). Always use ZIP_DEFLATED for smaller files.

Can I add files to an existing ZIP without extracting? ▼

Yes! Use 'a' mode (append) with ZipFile. You can add new files without extracting the entire archive: with zipfile.ZipFile("archive.zip", "a") as zipf: zipf.write("new_file.txt").

How do I check if a file is a valid ZIP? ▼

Use zipfile.is_zipfile(filename). It returns True if the file appears to be a valid ZIP archive. This is a quick way to check before trying to open it.

What's a common interview question about ZIP files? ▼

Common questions include: "How do you create a ZIP file in Python?" "What is the difference between ZIP_STORED and ZIP_DEFLATED?" and "How would you extract a specific file from a ZIP?"

Can I read a file from a ZIP without extracting it? ▼

Yes! Use the read() method: content = zipf.read("file.txt"). This returns the file content as bytes without creating a physical file on disk.

How do I zip an entire directory? ▼

Use os.walk() to traverse the directory and add each file to the ZIP. Use os.path.relpath() to preserve the directory structure inside the ZIP.

Where to Go From Here

Now that you can work with ZIP files, check out these related topics:

📝 Assignments

Practice what you've learned with assignments.

Learn More →

MySQL Database

Learn how to work with databases in Python.

Learn More →

Object-Oriented Programming

Learn about classes and objects in Python.

Learn More →
Interview Resources
  • Python Syntax & Variables Interview Questions
  • Top SQL Interview Questions & Answers
  • SQL Joins: Displaying Data from Multiple Tables FAQ
Previous: Binary Files Next: 📝 Assignments →