Skip to main content

Compression Program

There are many compression programs available for Python.

But, you can build your own.

How it works

  1. Find repetitive sequences of characters in a string.
  2. Replace it with smaller sized sequence of characters.
  3. Repeat until the string is compressed.

Actual algorithm

ZLIB module

There is a module called ZLIB that can be used to compress and decompress data.

pip install zlib

Run the command above in a terminal.

Run the code below in python file.

import zlib

Compressing

  1. Importing ZLIB module.
  2. Turning it into bytes (putting b in front of the string).
  3. Compressing the data.

The length is shortened by 19 bytes.

Decompressing

decompress() method can be used to decompress the data.