| 1 | <div align="center"> |
| 2 | <img alt="lzzzz" src="lzzzz.png" height="80" /> |
| 3 | |
| 4 | Full-featured [liblz4](https://github.com/lz4/lz4) binding for Rust |
| 5 | |
| 6 | [](https://crates.io/crates/lzzzz) |
| 7 | [](https://github.com/picoHz/lzzzz/blob/master/LICENSE) |
| 8 | [](https://docs.rs/lzzzz) |
| 9 |  |
| 10 | |
| 11 | </div> |
| 12 | |
| 13 | --- |
| 14 | |
| 15 | ## About |
| 16 | |
| 17 | Rust APIs for the [LZ4](https://lz4.github.io/lz4/) compression algorithm. |
| 18 | |
| 19 | - Supports almost all liblz4 features |
| 20 | - Zero dependencies except liblz4 |
| 21 | - Tested on Windows / macOS / Linux |
| 22 | |
| 23 | ## Usage |
| 24 | |
| 25 | Add this to your `Cargo.toml`: |
| 26 | |
| 27 | ```toml |
| 28 | [dependencies] |
| 29 | lzzzz = "1.0.3" |
| 30 | ``` |
| 31 | |
| 32 | [API Documentation](https://docs.rs/lzzzz) |
| 33 | |
| 34 | ## Features |
| 35 | |
| 36 | - LZ4 |
| 37 | - Compression (Block / Streaming) |
| 38 | - Decompression (Block / Streaming) |
| 39 | - Partial Decompression |
| 40 | - Custom Dictionary |
| 41 | - LZ4_HC |
| 42 | - Compression (Block / Streaming) |
| 43 | - Partial Compression |
| 44 | - Custom Dictionary |
| 45 | - LZ4F |
| 46 | - Compression |
| 47 | - Decompression |
| 48 | - Custom Dictionary |
| 49 | - Streaming I/O (`Read` / `BufRead` / `Write`) |
| 50 | |
| 51 | ## Examples |
| 52 | |
| 53 | ### Block Mode |
| 54 | |
| 55 | ```rust |
| 56 | use lzzzz::{lz4, lz4_hc, lz4f}; |
| 57 | |
| 58 | let data = b"The quick brown fox jumps over the lazy dog."; |
| 59 | |
| 60 | // LZ4 compression |
| 61 | let mut comp = Vec::new(); |
| 62 | lz4::compress_to_vec(data, &mut comp, lz4::ACC_LEVEL_DEFAULT)?; |
| 63 | |
| 64 | // LZ4_HC compression |
| 65 | let mut comp = Vec::new(); |
| 66 | lz4_hc::compress_to_vec(data, &mut comp, lz4_hc::CLEVEL_DEFAULT)?; |
| 67 | |
| 68 | // LZ4/LZ4_HC decompression |
| 69 | let mut decomp = vec![0; data.len()]; |
| 70 | lz4::decompress(&comp, &mut decomp)?; |
| 71 | |
| 72 | // LZ4F compression |
| 73 | let prefs = lz4f::Preferences::default(); |
| 74 | let mut comp = Vec::new(); |
| 75 | lz4f::compress_to_vec(data, &mut comp, &prefs)?; |
| 76 | |
| 77 | // LZ4F decompression |
| 78 | let mut decomp = Vec::new(); |
| 79 | lz4f::decompress_to_vec(&comp, &mut decomp)?; |
| 80 | ``` |
| 81 | |
| 82 | ### Streaming Mode |
| 83 | |
| 84 | ```rust |
| 85 | use lzzzz::{lz4, lz4_hc}; |
| 86 | |
| 87 | let data = b"The quick brown fox jumps over the lazy dog."; |
| 88 | |
| 89 | // LZ4 compression |
| 90 | let mut comp = lz4::Compressor::new()?; |
| 91 | let mut buf = Vec::new(); |
| 92 | comp.next_to_vec(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?; |
| 93 | |
| 94 | // LZ4_HC compression |
| 95 | let mut comp = lz4_hc::Compressor::new()?; |
| 96 | let mut buf = Vec::new(); |
| 97 | comp.next_to_vec(data, &mut buf)?; |
| 98 | |
| 99 | // LZ4/LZ4_HC decompression |
| 100 | let mut decomp = lz4::Decompressor::new()?; |
| 101 | let result = decomp.next(&data, data.len())?; |
| 102 | ``` |
| 103 | |
| 104 | ```rust |
| 105 | use lzzzz::lz4f::{WriteCompressor, ReadDecompressor, Preferences}; |
| 106 | use std::{fs::File, io::prelude::*}; |
| 107 | |
| 108 | // LZ4F Write-based compression |
| 109 | let mut f = File::create("foo.lz4")?; |
| 110 | let mut w = WriteCompressor::new(&mut f, Preferences::default())?; |
| 111 | w.write_all(b"Hello world!")?; |
| 112 | |
| 113 | // LZ4F Read-based decompression |
| 114 | let mut f = File::open("foo.lz4")?; |
| 115 | let mut r = ReadDecompressor::new(&mut f)?; |
| 116 | let mut buf = Vec::new(); |
| 117 | r.read_to_end(&mut buf)?; |
| 118 | ``` |