Nearby lessons

95 of 159

Python - ZIP Files

📌 What You Will Learn
  • Understand what a ZIP file is and why compression is useful
  • Import the zipfile module
  • Create a ZIP file using the ZipFile class with ZIP_DEFLATED
  • Add files to a ZIP archive using the write() method
  • Read a ZIP archive using namelist() and ZIP_STORED

Introduction to ZIP Files

A ZIP file is a compressed file that can store one or more files and folders.

Compression reduces the file size, making it easier to store and transfer files.

Python provides the built-in zipfile module for creating, reading, and extracting ZIP files.

Advantages of ZIP Files

  • Reduces file size.
  • Saves storage space.
  • Makes file transfer faster.
  • Allows multiple files to be stored in a single archive.
  • Easy to share files over the Internet.

The zipfile Module

Python provides the zipfile module to perform ZIP file operations.

🐍Code Cell
1from zipfile import *
Output
No output captured.

The ZipFile Class

The ZipFile class is used to create, read, and extract ZIP files.

Parameter Description
file_name Name of the ZIP file.
mode Specifies how the ZIP file should be opened.
compression Specifies the compression method.
🐍Code Cell
1ZipFile(file_name, mode, compression)
Output
No output captured.

Compression Types

The most commonly used compression constants are:

Constant Description
ZIP_STORED No compression is applied.
ZIP_DEFLATED Compresses files to reduce their size.

ZIP_DEFLATED

ZIP_DEFLATED is the most commonly used compression method.

It compresses the file before storing it inside the ZIP archive, thereby reducing the file size.

When to Use ZIP Files

  • Backing up files.
  • Sending multiple files through email.
  • Reducing storage space.
  • Distributing software packages.
  • Archiving project folders.

Program: Creating a ZIP File

🐍Code Cell
1from zipfile import *
2 
3f = ZipFile("files.zip", "w", ZIP_DEFLATED)
4 
5f.write("file1.txt")
6f.write("file2.txt")
7f.write("file3.txt")
8 
9f.close()
10 
11print("files.zip file created successfully")
Output
files.zip file created successfully

Explanation

  • ZipFile("files.zip", "w", ZIP_DEFLATED) creates a ZIP file named files.zip. The "w" mode opens it for writing, and ZIP_DEFLATED indicates that files should be compressed before being stored.
  • Each f.write(...) call adds one file into the ZIP archive.
  • f.close() closes the ZIP file after adding all files.

Performing Unzip Operation

To read or extract information from an existing ZIP file, create a ZipFile object in read mode.

ZIP_STORED represents the unzip operation. It is the default value, so specifying it is optional.

🐍Code Cell
1f = ZipFile("files.zip", "r", ZIP_STORED)
Output
No output captured.

The namelist() Method

After opening the ZIP file, we can obtain the names of all files stored inside it by using the namelist() method.

🐍Code Cell
1names = f.namelist()
Output
No output captured.

Program: Display All Files from a ZIP Archive

🐍Code Cell
1from zipfile import *
2 
3f = ZipFile("files.zip", "r", ZIP_STORED)
4 
5names = f.namelist()
6 
7for name in names:
8 
9 print("File Name:", name)
10 print("The Content of this file is:")
11 
12 f1 = open(name, "r")
13 
14 print(f1.read())
15 
16 print()
Output
No output captured.

Explanation

  • f.namelist() returns a list containing the names of all files present in the ZIP archive.
  • The for loop processes every file one by one.
  • Each file is opened in read mode and its complete contents are displayed.
📝 Key Takeaways
  • A ZIP file is a compressed archive that stores one or more files and folders
  • The ZipFile class creates, reads, and extracts ZIP files
  • ZIP_DEFLATED compresses files, while ZIP_STORED stores them without compression
  • The write() method adds a file to the ZIP archive
  • The namelist() method returns the names of all files stored inside the archive

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8