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
Spacedrive
Collaboration · File Storage
One file manager for all your devices and clouds — powered by a Virtual Distributed File System built in Rust.
Stalwart
Collaboration
All-in-one secure mail and collaboration server covering IMAP, JMAP, SMTP, CalDAV, CardDAV, and WebDAV in a single memory-safe Rust binary.
Temps
Analytics · Devops · Monitoring
A self-hosted Rust PaaS that replaces Vercel, Sentry, PostHog, Pingdom, Resend, and E2B with one binary — plus 440+ CLI operations agents like Claude Code can drive directly.
TensorZero
Ab Testing Experimentation · AI Development · Monitoring
TensorZero unifies the LLM gateway, observability, evaluation, optimization, and experimentation stack behind a single OpenAI-compatible API, built in Rust for sub-millisecond p99 latency.
Trieve
AI Development · Developer Tools · Search
All-in-one self-hostable platform for hybrid search, RAG, recommendations, and analytics built on Rust and Qdrant.
Vibe Kanban
AI Agents · AI Code Assistants · Project Management
A kanban board for planning work and dispatching Claude Code, Codex, Gemini CLI, and eight other coding agents into isolated git worktrees, then reviewing and merging their diffs from one UI.
Void
AI Code Assistants · Automation · Code Editors
Open-source AI code editor with direct LLM integration and data privacy
Zed
Code Editors · Collaboration · Developer Tools
High-performance, multiplayer code editor built in Rust by the creators of Atom and Tree-sitter, with native AI integration and real-time collaboration.