| 1 |
# miniz_oxide |
| 2 |
|
| 3 |
A fully safe, pure rust replacement for the [miniz](https://github.com/richgel999/miniz) DEFLATE/zlib encoder/decoder. |
| 4 |
The main intention of this crate is to be used as a back-end for the [flate2](https://github.com/alexcrichton/flate2-rs), but it can also be used on it's own. Using flate2 with the ```rust_backend``` feature provides an easy to use streaming API for miniz_oxide. |
| 5 |
|
| 6 |
The library is fully [no_std](https://docs.rust-embedded.org/book/intro/no-std.html). By default, the `with-alloc` feature is enabled, which requires the use of the `alloc` and `collection` crates as it allocates memory. |
| 7 |
|
| 8 |
The `std` feature additionally turns on things only available if `no_std` is not used. Currently this only means implementing [Error](https://doc.rust-lang.org/stable/std/error/trait.Error.html) for the `DecompressError` error struct returned by the simple decompression functions if enabled together with `with-alloc`. |
| 9 |
|
| 10 |
Using the library with `default-features = false` removes the dependency on `alloc` |
| 11 |
and `collection` crates, making it suitable for systems without an allocator. |
| 12 |
Running without allocation reduces crate functionality: |
| 13 |
|
| 14 |
- The `deflate` module is removed completely |
| 15 |
- Some `inflate` functions which return a `Vec` are removed |
| 16 |
|
| 17 |
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. |
| 18 |
|
| 19 |
miniz_oxide features no use of unsafe code. |
| 20 |
|
| 21 |
miniz_oxide can optionally be made to use a simd-accelerated version of adler32 via the [simd-adler32](https://crates.io/crates/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](https://crates.io/crates/adler) crate which features no unsafe code. |
| 22 |
|
| 23 |
## Usage |
| 24 |
Simple compression/decompression: |
| 25 |
```rust |
| 26 |
|
| 27 |
use miniz_oxide::deflate::compress_to_vec; |
| 28 |
use miniz_oxide::inflate::decompress_to_vec; |
| 29 |
|
| 30 |
fn roundtrip(data: &[u8]) { |
| 31 |
// Compress the input |
| 32 |
let compressed = compress_to_vec(data, 6); |
| 33 |
// Decompress the compressed input and limit max output size to avoid going out of memory on large/malformed input. |
| 34 |
let decompressed = decompress_to_vec_with_limit(compressed.as_slice(), 60000).expect("Failed to decompress!"); |
| 35 |
// Check roundtrip succeeded |
| 36 |
assert_eq!(data, decompressed); |
| 37 |
} |
| 38 |
|
| 39 |
fn main() { |
| 40 |
roundtrip("Hello, world!".as_bytes()); |
| 41 |
} |
| 42 |
|
| 43 |
``` |
| 44 |
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](https://github.com/alexcrichton/flate2-rs) which makes it easy to do streaming (de)compression or the low-level streaming functions instead. |