How to build a Python File Compressor

In this tutorial, we will be building a file compression program using python. Compression is the process of reducing the file size for ease of transmission, depending on the usage of the file. To know more about file compression check out this article on wikipedia.

For our tutorial, we will be using several libraries to make the file compressor. Firstly is gzip which is a python module used in data compression. Next, we will be using tqdm module to make cool progress bars.

Let’s first begin by installing tqdm. To install it run the command below.

pip3 install tqdm

The rest modules are batteries included so we need not install them.

Now let’s first begin by creating a file called main.py you can call it whatever you want. Now let’s import these modules inside our file.

from tqdm import tqdm
import zlib
import os

Next, we need to create a function that will handle our file compression and also update it with a progress bar.

def compress_file(file_path):
    #get the compressed file name
    compressed_file_path = file_path + '.gz'

    #open the original file in binary mode
    with open(file_path, 'rb') as file:

        #read the file
        with tqdm(total=os.path.getsize(file_path),unit='B', unit_scale=True, unit_divisor=1024) as pbar:
            file_contents = file.read()
            pbar.update(len(file_contents))

    # compress the file using zlib
    compressed_contents = zlib.compress(file_contents)

    # write the compressed content to the compressed file
    with open(compressed_file_path, 'wb') as file:
        file.write(compressed_contents)
    return compressed_file_path

The above function takes in the file path of the object to be compressed and compresses the file using the gzip library we imported above. The file is compressed and then we return the path of the compressed file.

Now let’s create a main function to call the above function.

def main():
    file_path = input("Enter the file path: ")
    compress_file(file_path)

The above function prompts the user for a file path to the file and then compresses it. Next, we need to add the script below to allow python to run the file directly.

if __name__ == '__main__':
    main()

Now save the file and run the command below.

python3 main.py

You will be prompted to enter a file path, enter the file path, and press enter to compress the file. You should see a new file with a .gz extension created.

There you have it, Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *