Skip to main content

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.

Metadata

Release files for zx0 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zx0 2.2.0
File Size Uploaded Provenance
zx0-2.2.0.tar.gz 16.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for zx0 2.2.0
File Interpreter ABI Platform
zx0-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.7 kB

Release files / zx0-2.2.0.tar.gz

Download URL zx0-2.2.0.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
beab3ee3205715bc430f808ffabade0548c6a60b07798faa67d5cc3d5e78a921
BLAKE2b-256 checksum
How to use checksums
b711b63f6e6ee3bdea1d353817d7d4f861a975bf74365409c74794977794a59a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / zx0-2.2.0-py3-none-any.whl

Download URL zx0-2.2.0-py3-none-any.whl
Size 20.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44c1f5c39c57e07e704f86a77b05a6eeabfacdd15df764ad9168a4fba844a06a
BLAKE2b-256 checksum
How to use checksums
d31e14724577b359cbb605fce297f0a4a50ae27d5ae6873d00e1f888f1f53950
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for zx0 2.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page