sevenz-rust2
| Language | Rust (edition 2024, Rust 1.93+) |
|---|---|
| License | Apache 2.0 |
| Repository | github.com/hasenbanck/sevenz-rust2 |
| Documentation | docs.rs/sevenz-rust2 |
| Depends on | lzma-rust2 and other Rust crates; the optional zstd feature links the C zstd library |
| Read / write | yes / yes; Copy, LZMA, LZMA2, BZip2, PPMd, Deflate, Zstandard, Brotli, LZ4, BCJ filters, Delta, AES-256; BCJ2 read only |
sevenz-rust2 is a 7z implementation in Rust: no 7-Zip library and, with the default features, no C code. It has one-call helpers (decompress_file, compress_to_path) and a lower-level API: Archive for the metadata, ArchiveReader to read entries, ArchiveWriter to create archives with a chosen chain of methods. It writes solid or non-solid archives, encrypts data and headers with AES-256, and decodes blocks on several threads.
Default features: aes256, bzip2, compress, ppmd, util. Enable zstd, brotli, lz4 or deflate for those methods. For WebAssembly use default-features = false, features = ["default_wasm"].
Installation#
cargo add sevenz-rust2
[dependencies]
sevenz-rust2 = { version = "0.24", features = ["zstd"] }
Basic usage: extract and create a 7z archive#
fn main() -> Result<(), sevenz_rust2::Error> {
// extract everything into a folder
sevenz_rust2::decompress_file("archive.7z", "output")?;
// pack the contents of a folder into a new archive (non-solid, LZMA2)
sevenz_rust2::compress_to_path("photos", "photos.7z")?;
Ok(())
}
How to list the files in a 7z archive#
Archive::open reads only the header:
use sevenz_rust2::Archive;
fn main() -> Result<(), sevenz_rust2::Error> {
let archive = Archive::open("archive.7z")?;
println!("solid: {}, blocks: {}", archive.is_solid, archive.blocks.len());
for entry in &archive.files {
println!("{} {} bytes {}", entry.name(), entry.size,
if entry.is_directory { "dir" } else { "" });
}
Ok(())
}
Archive::open_with_password(path, &password) opens archives with an encrypted header.
How to read a file from a 7z archive into memory#
use sevenz_rust2::{ArchiveReader, Password};
fn main() -> Result<(), sevenz_rust2::Error> {
let mut reader = ArchiveReader::open("archive.7z", Password::empty())?;
let data: Vec<u8> = reader.read_file("docs/readme.txt")?;
println!("{}", String::from_utf8_lossy(&data));
Ok(())
}
read_file decodes everything before the file in its solid block. To process many files, iterate once with for_each_entries:
let mut reader = ArchiveReader::open("archive.7z", Password::empty())?;
reader.for_each_entries(|entry, data| {
let mut buf = Vec::new();
data.read_to_end(&mut buf)?;
println!("{}: {} bytes", entry.name(), buf.len());
Ok(true) // false skips the rest of the current block
})?;
How to create and extract an encrypted 7z archive#
fn main() -> Result<(), sevenz_rust2::Error> {
sevenz_rust2::compress_to_path_encrypted("private", "secret.7z", "secret".into())?;
sevenz_rust2::decompress_file_with_password("secret.7z", "output", "secret".into())?;
Ok(())
}
How to choose methods, solid mode and add data from memory#
use sevenz_rust2::{encoder_options, ArchiveEntry, ArchiveWriter};
fn main() -> Result<(), sevenz_rust2::Error> {
let mut writer = ArchiveWriter::create("backup.7z")?;
writer.set_content_methods(vec![
encoder_options::AesEncoderOptions::new("secret".into()).into(), // optional encryption
encoder_options::Lzma2Options::from_level(9).into(),
]);
writer.set_encrypt_header(true); // hide file names
// a folder in one solid block (push_source_path_non_solid: one block per file)
writer.push_source_path("data", |_| true)?;
// a file from memory
let text = b"created by my app\n";
writer.push_archive_entry(ArchiveEntry::new_file("notes/readme.txt"), Some(&text[..]))?;
writer.finish()?; // writes the header
Ok(())
}
The closure passed to push_source_path filters paths: return false to skip a file.
Tips#
ArchiveReader::set_thread_countdecodes independent blocks in parallel.Archive::read_with_memory_limitandArchiveReader::new_with_memory_limitcap memory for untrusted archives.- Archives using Zstandard, Brotli or LZ4 only open in 7-Zip forks (7-Zip ZS, NanaZip) and in sevenz-rust2 itself; use LZMA2 for archives other people will open with standard 7-Zip.