# CLI-Anything Compression Strategy for .sketch ZIP Files: Deflate Algorithm Deep Dive

> Discover how CLI-Anything uses the Deflate algorithm to create compatible .sketch ZIP files. Learn about the ZIP DEFLATED compression strategy.

- Repository: [✨Data Intelligence Lab@HKU✨/CLI-Anything](https://github.com/HKUDS/CLI-Anything)
- Tags: deep-dive
- Published: 2026-08-16

---

**CLI-Anything generates `.sketch` archives using Python's standard deflate compression via `zipfile.ZipFile` with `compression=zipfile.ZIP_DEFLATED`, ensuring full compatibility with the Sketch application.**

The HKUDS/CLI-Anything repository automates CAD workflow generation by producing `.sketch` files as part of its preview bundle system. Understanding the compression strategy behind these ZIP archives reveals why the tool prioritizes the deflate algorithm for cross-platform reliability and Sketch native format adherence.

## The Deflate Compression Implementation

### Core ZIP Generation Logic

In [`cli-anything-plugin/preview_bundle.py`](https://github.com/HKUDS/CLI-Anything/blob/main/cli-anything-plugin/preview_bundle.py), the generator constructs `.sketch` archives by instantiating `zipfile.ZipFile` with the explicit parameter `compression=zipfile.ZIP_DEFLATED`. This selects the standard deflate algorithm, which provides lossless data compression universally supported by the Sketch application.

The implementation streams sketch data into an in-memory `BytesIO` buffer before finalizing the archive. This approach eliminates temporary disk I/O while maintaining the strict binary format that Sketch expects for its document containers.

### Architecture of the Compression Pipeline

Three critical files coordinate the `.sketch` compression strategy across the codebase:

- **[`cli-anything-plugin/preview_bundle.py`](https://github.com/HKUDS/CLI-Anything/blob/main/cli-anything-plugin/preview_bundle.py)** — Contains the core compression logic that invokes `zipfile.ZipFile(..., compression=zipfile.ZIP_DEFLATED)` to build the preview bundle.
- **[`cli-anything-plugin/skill_generator.py`](https://github.com/HKUDS/CLI-Anything/blob/main/cli-anything-plugin/skill_generator.py)** — Generates SKILL definitions that orchestrate calls to the preview bundle creation methods.
- **[`freecad/agent-harness/cli_anything/freecad/core/sketch.py`](https://github.com/HKUDS/CLI-Anything/blob/main/freecad/agent-harness/cli_anything/freecad/core/sketch.py)** — Prepares geometric data and JSON payloads that the preview bundle writer compresses into the final archive.

## Practical .sketch Generation Pattern

The following implementation demonstrates how CLI-Anything creates compressed `.sketch` files:

```python
import io
import zipfile

def make_sketch_zip(sketch_name: str, sketch_data: dict) -> bytes:
    """
    Produce a .sketch file (a ZIP archive) containing the required JSON payloads.
    The archive is compressed with the DEFLATE algorithm (ZIP_DEFLATED).
    """
    # In‑memory buffer for the ZIP

    zip_buffer = io.BytesIO()

    # Open the ZIP for writing using deflate compression

    with zipfile.ZipFile(zip_buffer, mode="w", compression=zipfile.ZIP_DEFLATED) as zf:
        # Each component of a Sketch file is a JSON file inside the archive

        for filename, payload in sketch_data.items():
            # Encode JSON payload to UTF‑8 bytes

            json_bytes = payload.encode("utf‑8")
            # Write it into the archive

            zf.writestr(f"{filename}.json", json_bytes)

    # Return the raw bytes of the .sketch ZIP

    return zip_buffer.getvalue()

```

In this pattern, `zipfile.ZIP_DEFLATED` ensures the output archive opens correctly in Sketch. The `writestr` method stores each JSON component as a separate file entry without requiring intermediate disk files.

## Compatibility and Performance Characteristics

The deflate compression strategy optimizes for **application compatibility** over maximum compression ratios. By using Python's standard library implementation rather than external dependencies, CLI-Anything guarantees consistent behavior across Windows, macOS, and Linux environments.

The in-memory `BytesIO` approach minimizes latency during CAD preview generation workflows. Each JSON payload undergoes UTF-8 encoding before compression, preserving international characters and geometric metadata within the `.sketch` container.

## Summary

- CLI-Anything employs **deflate compression** (`zipfile.ZIP_DEFLATED`) for all `.sketch` ZIP archives.
- The **[`cli-anything-plugin/preview_bundle.py`](https://github.com/HKUDS/CLI-Anything/blob/main/cli-anything-plugin/preview_bundle.py)** file implements the core compression logic.
- Archives are constructed in **memory buffers** (`BytesIO`) before persistence.
- This strategy ensures **native Sketch application compatibility** across platforms.

## Frequently Asked Questions

### Why does CLI-Anything use deflate instead of bzip2 or lzma for .sketch files?

CLI-Anything uses deflate because it is the standard compression method required for Sketch application compatibility and is natively supported by Python's `zipfile` module. Deflate offers an optimal balance between compression ratio and decompression speed, which is essential for rapid CAD preview generation.

### Which source file contains the primary compression logic for .sketch generation?

The primary implementation resides in [`cli-anything-plugin/preview_bundle.py`](https://github.com/HKUDS/CLI-Anything/blob/main/cli-anything-plugin/preview_bundle.py), which explicitly calls `zipfile.ZipFile` with `compression=zipfile.ZIP_DEFLATED`. The [`skill_generator.py`](https://github.com/HKUDS/CLI-Anything/blob/main/skill_generator.py) file coordinates the invocation of this logic, while [`freecad/agent-harness/cli_anything/freecad/core/sketch.py`](https://github.com/HKUDS/CLI-Anything/blob/main/freecad/agent-harness/cli_anything/freecad/core/sketch.py) prepares the underlying geometric data.

### How does CLI-Anything handle character encoding inside compressed .sketch files?

CLI-Anything encodes all JSON payloads as **UTF-8** bytes before writing them into the ZIP archive via `writestr()`. This ensures that international characters, mathematical symbols, and CAD metadata are preserved correctly within the `.sketch` container.

### Can developers adjust the compression level for larger sketch archives?

While the current implementation uses the default deflate compression level, Python's `zipfile` module supports the `compresslevel` parameter (ranging from 1 for fastest to 9 for maximum compression) when using `ZIP_DEFLATED`. Developers could modify [`preview_bundle.py`](https://github.com/HKUDS/CLI-Anything/blob/main/preview_bundle.py) to pass `compresslevel=9` for larger files requiring maximum compression.