Giter Club home page Giter Club logo

bzip2's Introduction

bzip2

This project is my implementation of bzip2 (wiki) — a popular and efficient data compression algorithm.

Here's how it zips Leo Tolstoy's "War and Peace" versus standard ZIP-algorithm:

preview

Table of contents

Features

  • a pure Python (>=3.10) implementation with no third-party dependencies
  • it outperforms (slightly) the standard zip-algorithm
  • it works with binary data, therefore no file-type restrictions

Disclaimer

Though the code presented is fully functional, passes all the tests and has notable compression efficiency, the following should be taken into account:

  • it's a pet project — it is made to satisfy my curiosity, it was never meant to be used in production
  • the file binary structure is incompatible with the original bzip2 format (= can't be opened with an archive manager app)
  • no consistency check (conversely to bzip canonical implementation)
  • optimization leaves much to be desired due to a variety of factors:
    • it's written on pure Python
    • algorithm parameters are not fine-tuned enough
  • only works with single files (so to compress a folder you have to tar it first)

Project overview

The project files can be roughly groupt into three cathegories:

  1. The implementation itself
  2. Infrastructure (Makefile, main.py, settings, tests)
  3. Extended documentation (all the README.md files)

Algorithm specification

bzip2 algorithm can be described as a chain of reversible transformations:

  1. Splitting into blocks
  2. RLE BWT MTF RLE HFC
  3. Merging the blocks

where:

term wiki description
RLE link run-length encoding
BWT link Burrows-Wheeler transform
MTF link move-to-front transform
HFC link Huffman coding

So, to encode (and compress) the file we apply the transformations from the list sequentially.

Thus to decode the file, one should apply the inverse transformations in inverse order.

Splitting into blocks

The Splitting into blocks step is just making an inerator based on the file descriptor given. This iterator yields byte-blocks of a fixed size (currently the default block size is 128 KiB).

Run-length encoding

See: RLE README (≈ 12 minutes to read)

Burrows-Wheeler transform

See: BWT README (≈ 20 minutes to read)

Move-to-front transform

See: MTF README.md (≈ 6 minutes to read)

Huffman coding

See: HFC README.md (≈ 20 minutes to read)

Merging the blocks

Merging the blocks is a little bit trickier. The final block size is indetermined, so we have to store it somewhere. Otherwise we won't be able to reverse this operation. The binary format is as follow:

 0               1               2               3
 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                  1st block size (4 bytes)                     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                1st block data (up to 4 GiB)                   |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                  2nd block size (4 bytes)                     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                2nd block data (up to 4 GiB)                   |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                             ...                               |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Project infrastructure

Software requirements

How to launch

  1. Choose a file you want to compress and copy its path to the clipboard.
  2. Paste it into the in_file variable in main.py.
  3. Run main from the project root folder.
    • or just run python app/main.py

How to setup developer environment

  • pip install -r requirements.dev.txt — to install all the dev dependencies
  • make lint — for formating and linting (isort => black => flake8 )
  • make test — to run fast tests
  • make test-all — to run all the tests (incloding the slow ones)

Licensing

MIT License

bzip2's People

Contributors

sentenzo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.