使用旧的 Rust 2015
1.0.127 |
|
---|
#28 在 #lz4
380KB
7.5K SLoC
lz4
此仓库包含 lz4 压缩库的绑定(https://github.com/Cyan4973/lz4)。
LZ4 是一种非常快的无损压缩算法,每个核心提供 400 MB/s 的压缩速度,具有接近线性的多线程应用可伸缩性。它还拥有一个极其快速的解码器,每个核心的速度可达多 GB/s,通常在多核系统上达到 RAM 速度限制。
用法
将以下内容放入您的 Cargo.toml
[dependencies]
lz4 = "1.23.1"
压缩/解压缩示例代码
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(())
}