Skip to main content

Python port of ZX0 v2.2 optimal data compressor

Project description

ZX0 - Python Port

This repository contains a Python port of ZX0, an optimal data compressor originally developed in C by Einar Saukas.

Original Project: You can find the original C implementation and more details here: https://github.com/einar-saukas/ZX0

Description

ZX0 is designed to find the optimal LZ77 parsing for data, particularly targeting scenarios where decompression speed and simplicity are important (like on Z80 microprocessors), while still achieving excellent compression ratios.

This Python version aims to replicate the functionality of the C implementation (specifically targeting v2.2 features) for understanding, experimentation, and use in Python-based environments.

Features (Mirrored from C v2.2)

  • Optimal Compression: Finds the theoretically optimal parsing according to the ZX0 cost model (default).
  • Quick Mode (-q): Uses a smaller offset limit (like ZX7) for faster, near-optimal compression.
  • Backwards Compression (-b): Compresses the input file starting from the end.
  • Skip Bytes (--skip N): Allows skipping a specified number of bytes at the beginning of the input file.
  • Classic Format (-c): Supports generating output compatible with the older v1 format (disables v2 inverted Elias Gamma bits).
  • Modern v2 Format: Default output format unless -c or -b is specified.

Requirements

  • Python 3.x (Tested with 3.12+, should work on most recent versions)

Installation

Normal pip installation

pip install zx0

Usage

ZX0 can be used either as a command-line tool or as a Python library in your own scripts.

Command-Line Usage

The main script is zx0.py. Run it from your terminal:

zx0 [options] input_file [output_file]

Arguments:

  • input_file: Path to the file you want to compress.
  • output_file (optional): Path for the compressed output. If omitted, defaults to <input_file>.zx0.

Options:

  • -f, --force: Force overwrite of the output file if it already exists.
  • -c, --classic: Use classic file format (v1.*).
  • -b, --backwards: Compress backwards.
  • -q, --quick: Use quick (non-optimal) compression mode.
  • --skip N: Skip the first N bytes of the input file.

Examples:

# Compress data.bin to data.bin.zx0
zx0 data.bin

# Compress image.raw to compressed.zx0
zx0 image.raw compressed.zx0

# Compress backwards and force overwrite
zx0 code.asm code.zx0 -b -f

# Compress using quick mode, skipping 64 bytes
zx0 level.map level.zx0 -q --skip 64

Library Usage

You can also use ZX0 as a library in your Python scripts:

from zx0 import zx0_compress

# Compress some data
data = b"Your binary data here"
compressed_data, stats = zx0_compress(data)

# Use the compressed data
print(f"Compressed from {stats['original_size']} to {stats['compressed_size']} bytes")
print(f"Compression ratio: {stats['ratio']:.4f}")

The zx0_compress function accepts the following parameters:

  • input_data (bytes or bytearray): The data to compress
  • skip (int, optional): Number of bytes to skip from the beginning. Default: 0
  • backwards (bool, optional): Compress backwards. Default: False
  • classic (bool, optional): Use classic file format (v1.*). Default: False
  • quick (bool, optional): Use quick non-optimal compression. Default: False

It returns a tuple containing:

  1. The compressed data as bytes
  2. A dictionary with compression statistics:
    • original_size: Size of the input data (after skip)
    • compressed_size: Size of the compressed data
    • delta: Difference between original and compressed sizes
    • duration: Time taken to compress in seconds
    • ratio: Compression ratio (compressed_size / original_size)

For more examples, see the examples directory.

Files

  • zx0.py: The main command-line interface script. Handles arguments, file I/O, and orchestrates the process.
  • optimize.py: Implements the core dynamic programming algorithm to find the optimal compression path.
  • compress.py: Takes the optimal path and generates the compressed bitstream according to the ZX0 format.
  • zx0_common.py: Contains shared constants, the Block class definition, and helper functions/classes used by other modules.

License

This Python port is licensed under the BSD 3-Clause License, consistent with the original C source code license. The individual .py files contain the following copyright notice and license terms, as required by the original license:

# This Python file is a translation/port of the ZX0 C implementation.
#
# Original C code Author: Einar Saukas
# Original C code License: BSD 3-Clause
#
# --- Start of Original C Code Notice ---
#
# (c) Copyright 2021 by Einar Saukas. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * The name of its author may not be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# --- End of Original C Code Notice ---

# This Python translation is also licensed under the BSD 3-Clause License,
# consistent with the original C code license.

Note

Please be aware that this Python implementation is primarily intended for educational purposes, experimentation, and integration within Python toolchains. Translation from C to python was done using AI model and this implementation is not well tested.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zx0-2.2.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zx0-2.2.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file zx0-2.2.0.tar.gz.

File metadata

  • Download URL: zx0-2.2.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zx0-2.2.0.tar.gz
Algorithm Hash digest
SHA256 beab3ee3205715bc430f808ffabade0548c6a60b07798faa67d5cc3d5e78a921
MD5 620ea2e01429e1a8d0b80bee6ac97de3
BLAKE2b-256 b711b63f6e6ee3bdea1d353817d7d4f861a975bf74365409c74794977794a59a

See more details on using hashes here.

File details

Details for the file zx0-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: zx0-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for zx0-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44c1f5c39c57e07e704f86a77b05a6eeabfacdd15df764ad9168a4fba844a06a
MD5 a3fe22f539bcdfcb1841aca500c549b6
BLAKE2b-256 d31e14724577b359cbb605fce297f0a4a50ae27d5ae6873d00e1f888f1f53950

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page