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.

Loading editor…

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.
Loading editor…

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

Loading editor…

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.

Loading editor…

The namelist() Method

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

Loading editor…

Program: Display All Files from a ZIP Archive

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.