#deflate #zlib #miniz #encoding

no-std miniz_oxide

DEFLATE compression and decompression library rewritten in Rust based on miniz

32 releases

0.8.0 Aug 8, 2024
0.7.4 Jun 18, 2024
0.7.3 May 17, 2024
0.7.2 Feb 3, 2024
0.1.1 Oct 28, 2017

#4 in Compression

Download history 2376639/week @ 2024-07-19 2323722/week @ 2024-07-26 2291402/week @ 2024-08-02 2478776/week @ 2024-08-09 2523226/week @ 2024-08-16 2772743/week @ 2024-08-23 2708442/week @ 2024-08-30 2797502/week @ 2024-09-06 2532268/week @ 2024-09-13 2764043/week @ 2024-09-20 2737625/week @ 2024-09-27 2767158/week @ 2024-10-04 2663670/week @ 2024-10-11 2916651/week @ 2024-10-18 2704279/week @ 2024-10-25 2153683/week @ 2024-11-01

10,922,328 downloads per month
Used in 40,358 crates (107 directly)

MIT OR Zlib OR Apache-2.0

225KB
4.5K SLoC

miniz_oxide

A fully safe, pure rust replacement for the miniz DEFLATE/zlib encoder/decoder. The main intention of this crate is to be used as a back-end for the flate2, but it can also be used on its own. Using flate2 with the rust_backend feature provides an easy to use streaming API for miniz_oxide.

The library is fully no_std. By default, the with-alloc feature is enabled, which requires the use of the alloc and collection crates as it allocates memory.

The std feature additionally turns on things only available if no_std is not used. Currently this only means implementing Error for the DecompressError error struct returned by the simple decompression functions if enabled together with with-alloc.

Using the library with default-features = false removes the dependency on alloc and collection crates, making it suitable for systems without an allocator. Running without allocation reduces crate functionality:

  • The deflate module is removed completely
  • Some inflate functions which return a Vec are removed

miniz_oxide 0.5.x and 0.6.x Requires at least rust 1.40.0 0.3.x requires at least rust 0.36.0.

miniz_oxide features no use of unsafe code.

miniz_oxide can optionally be made to use a simd-accelerated version of adler32 via the simd-adler32 crate by enabling the 'simd' feature. This is not enabled by default as due to the use of simd intrinsics, the simd-adler32 has to use unsafe. The default setup uses the adler crate which features no unsafe code.

Usage

Simple compression/decompression:


use miniz_oxide::deflate::compress_to_vec;
use miniz_oxide::inflate::decompress_to_vec;

fn roundtrip(data: &[u8]) {
    // Compress the input
    let compressed = compress_to_vec(data, 6);
    // Decompress the compressed input and limit max output size to avoid going out of memory on large/malformed input.
    let decompressed = decompress_to_vec_with_limit(compressed.as_slice(), 60000).expect("Failed to decompress!");
    // Check roundtrip succeeded
    assert_eq!(data, decompressed);
}

fn main() {
    roundtrip("Hello, world!".as_bytes());
}

These simple functions will do everything in one go and are thus not recommended for use cases outside of prototyping/testing as real world data can have any size and thus result in very large memory allocations for the output Vector. Consider using miniz_oxide via flate2 which makes it easy to do streaming (de)compression or the low-level streaming functions instead.

Dependencies

~230KB