LZ4 compression library for Rust - vendored from GitHub
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Patrick Marks 287dbd063a
Some checks failed
Test / test-nix (macos-latest) (push) Has been cancelled
Test / test-nix (ubuntu-latest) (push) Has been cancelled
Test / test-windows (push) Has been cancelled
safety note & bump version
2025-01-07 10:23:13 +01:00
.github/workflows lz4-sys: add libc shim for wasm targets (#49) 2024-09-06 10:12:13 +02:00
lz4-sys explicity on riscv64 march (#57) 2025-01-07 10:18:05 +01:00
src safety note & bump version 2025-01-07 10:23:13 +01:00
.gitattributes Begin rust project 2015-02-28 12:28:32 +03:00
.gitignore Dependency update 2017-05-31 21:59:21 +03:00
.gitmodules update liblz4 to 1.7.5 2017-02-14 16:44:51 -08:00
Cargo.toml safety note & bump version 2025-01-07 10:23:13 +01:00
CHANGELOG.md bump versions for l4z 1.10 (#47) 2024-07-22 12:39:19 -07:00
LICENSE Begin rust project 2015-02-28 12:28:32 +03:00
README.md Update badges in README 2020-06-13 13:10:53 +03:00

lz4

Build Status Crates.io Join the chat at https://gitter.im/bozaro/lz4-rs Rustdoc

NOTE: 10xGenomics is the new official home of lz4-rs, replacing https://github.com/bozaro/lz4-rs

This repository contains binding for lz4 compression library (https://github.com/Cyan4973/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.

Usage

Put this in your Cargo.toml:

[dependencies]
lz4 = "1.23.1"

Sample code for compression/decompression:

extern crate lz4;

use std::env;
use std::fs::File;
use std::io::{self, Result};
use std::path::{Path, PathBuf};

use lz4::{Decoder, EncoderBuilder};

fn main() {
    println!("LZ4 version: {}", lz4::version());

    for path in env::args().skip(1).map(PathBuf::from) {
        if let Some("lz4") = path.extension().and_then(|e| e.to_str()) {
            decompress(&path, &path.with_extension("")).unwrap();
        } else {
            compress(&path, &path.with_extension("lz4")).unwrap();
        }
    }
}

fn compress(source: &Path, destination: &Path) -> Result<()> {
    println!("Compressing: {} -> {}", source.display(), destination.display());

    let mut input_file = File::open(source)?;
    let output_file = File::create(destination)?;
    let mut encoder = EncoderBuilder::new()
        .level(4)
        .build(output_file)?;
    io::copy(&mut input_file, &mut encoder)?;
    let (_output, result) = encoder.finish();
    result
}

fn decompress(source: &Path, destination: &Path) -> Result<()> {
    println!("Decompressing: {} -> {}", source.display(), destination.display());

    let input_file = File::open(source)?;
    let mut decoder = Decoder::new(input_file)?;
    let mut output_file = File::create(destination)?;
    io::copy(&mut decoder, &mut output_file)?;

    Ok(())
}