Sai A Sai A
Updated date Feb 21, 2024
In this blog, we will learn how to create a file compression program in Python using various methods, including zlib, gzip, and zipfile modules.

Introduction:

File compression is an essential aspect of modern computing, allowing us to reduce the size of large files and folders for easier storage and transmission. In this blog post, we will explore how to create a simple file compression program in Python

Method 1: Using the zlib Module

The zlib module in Python provides functions for compression and decompression using the zlib library. Let's start by creating a basic file compression program using zlib.

import zlib

def compress_file(input_file, output_file):
    with open(input_file, 'rb') as f_in:
        data = f_in.read()
        compressed_data = zlib.compress(data)
        with open(output_file, 'wb') as f_out:
            f_out.write(compressed_data)

input_file = 'example.txt'
output_file = 'example_compressed.txt'

compress_file(input_file, output_file)
print("File compression successful!")

Output:

File compression successful!

In this method, we first import the zlib module, which provides compression and decompression functions. We define a function compress_file that takes an input file and an output file as arguments. Inside the function, we open the input file in binary read mode ('rb') and read its contents. We then use zlib.compress() to compress the data. Finally, we write the compressed data to the output file in binary write mode ('wb'). Upon execution, the program compresses the file and prints a success message.

Method 2: Using the gzip Module

Another popular method for file compression in Python is using the gzip module, which provides functions specifically for gzip file compression.

import gzip

def compress_file(input_file, output_file):
    with open(input_file, 'rb') as f_in:
        with gzip.open(output_file, 'wb') as f_out:
            f_out.writelines(f_in)

input_file = 'example.txt'
output_file = 'example_compressed.txt.gz'

compress_file(input_file, output_file)
print("File compression successful!")

Output:

File compression successful!

In this method, we import the gzip module and define a function compress_file. Inside the function, we open the input file in binary read mode ('rb') and use gzip.open() to open the output file in gzip compression mode ('wb'). We then write the lines from the input file directly to the gzip file. This method simplifies the compression process, as gzip.open() handles both compression and file writing simultaneously.

Method 3: Using the zipfile Module

The zipfile module in Python allows us to work with ZIP archives, which can contain one or more compressed files. Let's see how to compress a file using the zipfile module.

import zipfile

def compress_file(input_file, output_file):
    with zipfile.ZipFile(output_file, 'w', compression=zipfile.ZIP_DEFLATED) as zipf:
        zipf.write(input_file)

input_file = 'example.txt'
output_file = 'example_compressed.zip'

compress_file(input_file, output_file)
print("File compression successful!")

Output:

File compression successful!

In this method, we import the zipfile module and define a function compress_file. Inside the function, we create a new ZipFile object with the output file name and mode ('w'). We specify the compression method using the compression parameter (in this case, zipfile.ZIP_DEFLATED, which indicates using the deflate compression algorithm). Finally, we use the write() method to add the input file to the ZIP archive. Upon execution, the program compresses the file into a ZIP archive.

Conclusion:

In this blog, we have explored multiple methods for file compression in Python using the zlib, gzip, and zipfile modules. Each method offers a different approach to achieving compression, whether through low-level compression libraries or high-level abstractions. By understanding these methods, you can choose the most suitable approach for your compression needs.

Comments (0)

There are no comments. Be the first to comment!!!