This repository contains binding for lz4 compression library (https://code.google.com/p/lz4/).
LZ4 is a very fast lossless compression algorithm, providing compression speed at 400 MB/s per core, with near-linear scalability for multi-threaded applications. It also features an extremely fast decoder, with speed in multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
Put this in your Cargo.toml
:
toml
[dependencies]
lz4 = "*"
And this in your crate root:
rust
extern crate lz4;
Sample code for compression/decompression: ```rust
extern crate lz4;
use std::os; use std::oldpath::Path; use std::oldio::fs::File; use std::oldio::IoResult; use std::oldio::IoErrorKind; use std::oldio::Reader; use std::oldio::Writer;
fn main() { let suffix = ".lz4"; for arg in os::args()[1..].iter() { if arg.endswith(suffix) { decompress(&Path::new(arg), &Path::new(&arg[0..arg.len()-suffix.len()])).unwrap(); } else { compress(&Path::new(arg), &Path::new(&(arg.tostring() + suffix))).unwrap(); } } }
fn compress(src: &Path, dst: &Path) -> IoResult<()> { println!("Compressing: {:?} -> {:?}", src, dst); let mut fi = try!(File::open(src)); let mut fo = try!(lz4::Encoder::new(try!(File::create(dst)), 0)); try!(copy(&mut fi, &mut fo)); match fo.finish() { (_, result) => result } }
fn decompress(src: &Path, dst: &Path) -> IoResult<()> { println!("Decompressing: {:?} -> {:?}", src, dst); let mut fi = try!(lz4::Decoder::new(File::open(src))); let mut fo = try!(File::create(dst)); copy(&mut fi, &mut fo) }
fn copy(src: &mut Reader, dst: &mut Writer) -> IoResult<()> { let mut buffer: [u8; 1024] = [0; 1024]; loop { let len = match src.read(&mut buffer) { Ok(len) => len, Err(ref e) if e.kind == IoErrorKind::EndOfFile => 0, Err(e) => return Err(e) }; if len == 0 { break; } try!(dst.write_all(&buffer[0..len])); } Ok(()) } ```