flate2
Streaming DEFLATE, gzip, and zlib compression for Rust with pluggable backends
Repository Health
Technical Analysis
flate2 is the de facto compression library for Rust, providing encoders and decoders for the DEFLATE, zlib, and gzip formats behind familiar Read, BufRead, and Write stream adapters. It wraps low-level compression engines in ergonomic types like GzEncoder, ZlibDecoder, and DeflateEncoder, so you can compress or decompress data by simply wrapping an existing stream.
Under the hood, flate2 supports multiple interchangeable backends selected via Cargo features: the pure-Rust miniz_oxide (default, no C compiler required), the high-performance zlib-rs rewrite, and the system zlib or zlib-ng C libraries. This lets projects trade off portability, performance, and bit-for-bit compatibility without changing their application code.
What You Get
- Encoder and decoder types for DEFLATE, zlib, and gzip in
read,bufread, andwritevariants - A pure-Rust default backend (
miniz_oxide) that needs no C compiler and uses only safe Rust - Optional high-performance backends:
zlib-rs, systemzlib, andzlib-ng, selectable via Cargo features - Low-level
Compress/Decompressin-memory stream primitives for maximum control - A
GzBuilderfor constructing gzip streams with custom headers and metadata
Common Use Cases
- Transparently gzip-compressing or decompressing HTTP request and response bodies
- Reading and writing
.gzfiles without shelling out to external tools - Compressing data before storing it in a database, cache, or on disk
- Decoding zlib- or DEFLATE-compressed payloads inside binary file formats and protocols
Under The Hood
Architecture
flate2 is organized around three format modules — deflate, zlib, and gz — each mirroring the same read.rs/bufread.rs/write.rs layout so every format offers Read, BufRead, and Write adapters with an identical shape (src/deflate, src/zlib, src/gz). These stream types delegate to the raw Compress/Decompress primitives in src/mem.rs, which in turn drive a pluggable backend selected in src/ffi through the Backend, DeflateBackend, and InflateBackend traits. The src/ffi directory holds one implementation per backend (miniz_oxide.rs, zlib_rs.rs, c.rs), so the format and I/O layers stay backend-agnostic while mem.rs and zio.rs handle buffering and the compress/flush/finish state machine.
Tech Stack
Written in pure Rust (edition 2018, rust-version 1.67), flate2 depends on miniz_oxide for the default safe-Rust backend, crc32fast for checksums, and optional libz-sys, libz-ng-sys, and zlib-rs crates for alternate backends, all gated behind additive Cargo features. Backend selection is resolved entirely at compile time, and document-features is used to generate the feature-flag documentation.
Code Quality
The crate is mature and well tested: integration tests cover capabilities, early flushing, empty reads, zero-length writes, and gzip decoding (tests/), and a fuzz/ harness plus quickcheck property tests exercise edge cases. Public types carry extensive rustdoc with runnable examples, and the 27 files under examples/ demonstrate every encoder/decoder variant. The layered design keeps unsafe confined to the C and zlib-rs FFI backends, with the default path staying entirely in safe Rust.
API Design
The public API is highly idiomatic: encoders and decoders implement std::io::Read/Write, so they compose with the rest of the Rust I/O ecosystem with zero glue code — you wrap a stream, read or write bytes, and call finish(). Naming is consistent and predictable (GzEncoder, ZlibDecoder, DeflateEncoder) across all three format modules, and getting started requires only a couple of lines. The Compression level type and GzBuilder provide finer control when needed without complicating the common case.
Used by 35 apps in this directory
melty
AI Code Assistants · Code Editors · Developer Tools
The AI code editor where every chat message is a git commit you can revert, branch, or squash
mesh-llm
AI Agents · AI Development
Mesh LLM pools GPUs and memory across every machine you own into one OpenAI-compatible API, so agents tap distributed compute instead of a single GPU box or a metered cloud bill.
Mistle
AI Agents · Developer Tools
Self-hostable platform for running autonomous coding agents in isolated, credentialless sandboxes with brokered credentials, reusable snapshots, and event-driven triggers.
OpenPencil
AI Design Tools
An open-source, AI-native vector design tool with concurrent agent teams, design-as-code, a built-in MCP server, and multi-model intelligence — a distinct project from the similarly-named Figma-file-reading "open-pencil" editor.
OpenShell
AI Agents · Developer Tools
The safe, private runtime that lets autonomous AI agents operate in sandboxed environments governed by declarative YAML policies — blocking data exfiltration, credential leaks, and unauthorized network activity before they happen.
OpenViking
AI Development · AI Memory · Databases
An open-source context database that gives AI agents a unified filesystem for memory, resources, and skills with hierarchical tiered retrieval.
PostgresML
AI Development · Databases
Run ML training and LLM inference natively inside PostgreSQL with GPU acceleration — no data movement required.
Qdrant
AI Development · Databases · Search
Open-source vector database and search engine built in Rust for production-grade AI applications — from semantic search to RAG pipelines and recommendation systems.
QuestDB
Analytics · Databases
A high-performance, open-source time-series database built for financial market data, IoT telemetry, and real-time analytics, combining a zero-GC Java/C++ core with SIMD-accelerated SQL and a WAL-to-Parquet storage engine.