Back to Python
2025-12-045 min read

File Compression (Python Programming)

Learn File Compression (Python Programming) step by step with clear examples and exercises.

Why This Matters

In today's data-driven world, managing large amounts of data is an essential task for developers. File compression plays a crucial role in this process by reducing storage space and facilitating faster file transfers. Compressed files take less time to download and consume less memory, making them ideal for sharing large datasets or archiving important documents. Python, with its extensive libraries, offers multiple ways to compress and decompress files, which we will explore in this lesson.

Why This Matters

File compression is a fundamental skill that helps manage storage space and transfer files efficiently. Compressed files take less time to download and consume less memory, making them ideal for sharing large datasets or archiving important documents. Python, with its extensive libraries, offers multiple ways to compress and decompress files, which we will explore in this lesson.

Advantages of File Compression

  • Space Saving: Compressed files take up significantly less space on disk, making them ideal for storing large datasets or archiving important documents.
  • Faster Transfers: Compressed files are smaller in size, which means they can be transferred faster than their uncompressed counterparts.
  • Efficient Storage: By compressing data before storing it, you can make better use of your storage resources and reduce the need for additional storage space.

Prerequisites

To fully understand the concepts covered in this lesson, you should be familiar with:

  • Basic Python syntax (variables, loops, functions)
  • Understanding of data structures like lists and dictionaries
  • Familiarity with file handling operations (reading, writing)

Core Concept

Python provides several libraries for handling compression and decompression tasks. The most commonly used ones are gzip, bz2, zipfile, and lzma. Each library offers its own unique advantages in terms of compression ratio, speed, and file format support.

gzip

gzip is a fast lossless data compression library that uses the Lempel-Ziv coding technique. It is widely used for compressing and decompressing files with the .gz extension. The gzip module in Python allows us to work with .gz files easily.

import gzip

Compress a file

def compress_file(input_filename, output_filename):

with open(input_filename, 'rb') as f_in:

with gzip.open(output_filename, 'wb') as f_out:

f_out.writelines(f_in)

Decompress a file

def decompress_file(input_filename, output_filename):

with gzip.open(input_filename, 'rb') as f_in:

with open(output_filename, 'wb') as f_out:

f_out.writelines(f_in)


### bz2

The `bz2` module in Python is another lossless compression library that utilizes the Burrows-Wheeler algorithm. It is similar to `gzip`, but offers better compression for certain types of data. The file extension for `bz2` compressed files is `.bz2`.

import bz2

Compress a file

def compress_file(input_filename, output_filename):

with open(input_filename, 'rb') as f_in:

with bz2.open(output_filename, 'wb') as f_out:

f_out.writelines(f_in)

Decompress a file

def decompress_file(input_filename, output_filename):

with bz2.open(input_filename, 'rb') as f_in:

with open(output_filename, 'wb') as f_out:

f_out.writelines(f_in)


### zipfile

The `zipfile` module enables us to work with the ZIP file format (`.zip`). It supports both compression and decompression of files, as well as adding and extracting individual files from a ZIP archive.

import zipfile

Create a new ZIP archive

def create_zip(input_filenames, output_filename):

with zipfile.ZipFile(output_filename, 'w', compression=zipfile.ZIP_DEFLATED) as zf:

for filename in input_filenames:

zf.write(filename)

Extract files from a ZIP archive

def extract_zip(input_filename, output_directory):

with zipfile.ZipFile(input_filename) as zf:

zf.extractall(path=output_directory)


### lzma

The `lzma` module in Python provides support for the LZMA compression algorithm, which offers high compression ratios. The file extension for `lzma` compressed files is `.xz`.

import lzma

Compress a file

def compress_file(input_filename, output_filename):

with open(input_filename, 'rb') as f_in:

with lzma.open(output_filename, 'wb') as f_out:

f_out.writelines(f_in)

Decompress a file

def decompress_file(input_filename, output_filename):

with lzma.open(input_filename) as f_in:

with open(output_filename, 'wb') as f_out:

f_out.writelines(f_in)

Worked Example

Let's compress a text file using gzip, bz2, and lzma, then decompress each of them back to its original form.

  1. Create a sample text file:
echo "Hello, World!" > sample.txt
  1. Compress the file using gzip, bz2, and lzma:
python compress_file.py sample.txt gzipped_sample.gz
python compress_file.py sample.txt bzipped_sample.bz2
python compress_file.py sample.txt lzma_compressed_sample.xz
  1. Decompress the files back to their original form:
python decompress_file.py gzipped_sample.gz decompressed_sample_gzip.txt
python bz2decode bzipped_sample.bz2 > decompressed_sample_bz2.txt
unxz lzma_compressed_sample.xz > decompressed_sample_lzma.txt

Now, compare the sizes of the original file and the compressed files:

ls -lh sample.*

You should see that the compressed files are significantly smaller than the original text file.

Common Mistakes

  • Forgetting to close files: Always ensure you close all opened files using a with statement to avoid leaking resources.
  • Not handling exceptions: Make sure to handle potential errors, such as when a file cannot be found or read, by wrapping the relevant code in a try-except block.
  • Incorrect compression level: Adjust the compression level (e.g., ZIP_DEFLATED in zipfile) to find the optimal balance between compression ratio and processing speed.

Common Mistakes - Additional Details

  • Using incorrect file extensions: Make sure to use the correct file extension (.gz, .bz2, .zip, or .xz) when compressing and decompressing files.
  • Not handling errors properly: If an error occurs during compression or decompression, it's essential to handle these errors gracefully and provide meaningful error messages to the user.

Practice Questions

  1. Write a Python script that compresses multiple files into a single ZIP archive.
  2. Write a Python script that decompresses a ZIP archive containing multiple files and extracts them to separate directories based on their original folder structure.
  3. Given a text file, write a script that compares the compression ratios of gzip, bz2, and lzma for the file.
  4. Write a Python script that compresses a directory containing multiple files and subdirectories into a single ZIP archive.
  5. Write a Python script that decompresses a ZIP archive and saves each extracted file to a separate subdirectory based on its original folder structure.

FAQ

Why are my compressed files larger than expected?

Compression algorithms may not always achieve the maximum possible compression ratio due to factors such as the nature of the data, implementation details, or specific settings used. Adjusting the compression level or trying a different library might help improve the compression ratio.

How can I find out which compression algorithm was used on a file?

You can use various tools like file (available on Unix-like systems) to determine the compression format of a file based on its header:

file compressed_file.gz

Can I compress a directory using Python?

Yes! You can create a ZIP archive containing all files and subdirectories within a given directory by modifying the create_zip() function in the Core Concept section to recursively traverse the target directory.

How do I handle errors during compression or decompression in Python?

To handle errors during compression or decompression, you can use try-except blocks to catch and handle exceptions. For example:

try:

Compression or decompression code here

except Exception as e:

print(f"An error occurred: {e}")

File Compression (Python Programming) | Python | XQA Learn