Rust 7z压缩解压库sevenz-rust的使用:高效处理7z格式文件的Rust插件库

Rust 7z压缩解压库sevenz-rust的使用:高效处理7z格式文件的Rust插件库

这是一个用纯Rust编写的7z压缩/解压器,很大程度上受到了apache commons-compress项目的启发。

LZMA/LZMA2解码器和所有过滤器代码都是从tukaani xz for java移植过来的。

解压缩

支持的编解码器:

  • ✔️ BZIP2 (需要’bzip2’特性)
  • ✔️ COPY
  • ✔️ LZMA
  • ✔️ LZMA2
  • ✔️ ZSTD (需要’zstd’特性)

支持的过滤器:

  • ✔️ BCJ X86
  • ✔️ BCJ PPC
  • ✔️ BCJ IA64
  • ✔️ BCJ ARM
  • ✔️ BCJ ARM_THUMB
  • ✔️ BCJ SPARC
  • ✔️ DELTA
  • ✔️ BJC2

使用

在Cargo.toml中添加依赖:

[dependencies]
sevenz-rust={version="0.2"}

将源文件"data/sample.7z"解压到目标路径"data/sample":

sevenz_rust::decompress_file("data/sample.7z", "data/sample").expect("complete");

解压加密的7z文件

添加’aes256’特性:

[dependencies]
sevenz-rust={version="0.2", features=["aes256"]}
sevenz_rust::decompress_file_with_password(
    "path/to/encrypted.7z", 
    "path/to/output", 
    "password".into()
).expect("complete");

多线程解压

参考examples/mt_decompress.rs

压缩

目前仅支持LZMA2方法。

[dependencies]
sevenz-rust={version="0.5.0", features=["compress"]}

使用辅助函数创建7z文件:

sevenz_rust::compress_to_path(
    "examples/data/sample", 
    "examples/data/sample.7z"
).expect("compress ok");

使用AES加密

需要版本>=0.3.0

[dependencies]
sevenz-rust={version="0.5", features=["compress","aes256"]}

使用辅助函数创建带密码的7z文件:

sevenz_rust::compress_to_path_encrypted(
    "examples/data/sample", 
    "examples/data/sample.7z", 
    "password".into()
).expect("compress ok");

高级用法

[dependencies]
sevenz-rust={version="0.5.0", features=["compress","aes256"]}

固体压缩

use sevenz_rust::*;

let mut sz = SevenZWriter::create("dest.7z").expect("create writer ok");
sz.push_source_path("path/to/compress", |_| true).expect("pack ok");
sz.finish().expect("compress ok");

压缩方法

带加密和lzma2选项:

use sevenz_rust::*;

let mut sz = SevenZWriter::create("dest.7z").expect("create writer ok");
sz.set_content_methods(vec![
    sevenz_rust::AesEncoderOptions::new("sevenz-rust".into()).into(),
    lzma::LZMA2Options::with_preset(9).into(),
]);
sz.push_source_path("path/to/compress", |_| true).expect("pack ok");
sz.finish().expect("compress ok");

完整示例代码

基础解压示例

use sevenz_rust::decompress_file;

fn main() {
    // 解压7z文件
    decompress_file("test.7z", "output_dir").expect("解压失败");
    println!("解压成功!");
}

带密码解压示例

use sevenz_rust::decompress_file_with_password;

fn main() {
    // 解压加密的7z文件
    decompress_file_with_password(
        "encrypted.7z",
        "output_dir",
        "mypassword".into()
    ).expect("解压失败");
    println!("解压成功!");
}

基础压缩示例

use sevenz_rust::compress_to_path;

fn main() {
    // 压缩文件夹
    compress_to_path("source_dir", "output.7z").expect("压缩失败");
    println!("压缩成功!");
}

带密码压缩示例

use sevenz_rust::compress_to_path_encrypted;

fn main() {
    // 加密压缩文件夹
    compress_to_path_encrypted(
        "source_dir",
        "output.7z",
        "mypassword".into()
    ).expect("压缩失败");
    println!("加密压缩成功!");
}

高级压缩配置示例

use sevenz_rust::*;
use sevenz_rust::lzma;

fn main() {
    // 创建7z写入器
    let mut sz = SevenZWriter::create("advanced.7z").expect("创建写入器失败");
    
    // 设置压缩方法(AES加密+LZMA2)
    sz.set_content_methods(vec![
        AesEncoderOptions::new("password123".into()).into(),
        lzma::LZMA2Options::with_p preset(9).into(),
    ]);
    
    // 添加要压缩的路径
    sz.push_source_path("data_to_compress", |_| true).expect("添加压缩内容失败");
    
    // 完成压缩
    sz.finish().expect("压缩失败");
    println!("高级压缩成功!");
}

1 回复

Rust 7z压缩解压库sevenz-rust的使用指南

简介

sevenz-rust是一个用于处理7z格式文件的纯Rust库,提供了压缩和解压7z文件的功能。它不依赖外部二进制文件或系统库,完全在Rust生态中实现。

主要特性

  • 支持7z格式的压缩和解压
  • 纯Rust实现,无外部依赖
  • 支持多种压缩算法(LZMA/LZMA2等)
  • 支持密码保护的压缩文件
  • 跨平台支持

安装

在Cargo.toml中添加依赖:

[dependencies]
sevenz-rust = "0.2"

基本使用方法

解压7z文件

use sevenz_rust::SevenZArchive;
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 解压文件
    SevenZArchive::extract(
        Path::new("archive.7z"), 
        Path::new("output_dir")
    )?;
    Ok(())
}

带密码解压

use sevenz_rust::{SevenZArchive, Password};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    SevenZArchive::extract_with_password(
        "archive.7z", 
        "output_dir", 
        Password::from("mypassword")
    )?;
    Ok(())
}

创建7z压缩文件

use sevenz_rust::{SevenZArchive, SevenZArchiveEntry, SevenZMethod};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut archive = SevenZArchive::create("output.7z")?;
    
    // 添加文件到压缩包
    archive.push_archive_entry(
        SevenZArchiveEntry::from_path(Path::new("file1.txt"), "file1.txt")?
            .set_method(SevenZMethod::LZMA)
    );
    
    // 添加整个目录
    archive.push_source_dir(Path::new("docs"), "docs")?;
    
    archive.write()?;
    Ok(())
}

带密码压缩

use sevenz_rust::{SevenZArchive, SevenZArchiveEntry, SevenZMethod, Password};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut archive = SevenZArchive::create_with_password(
        "secure.7z", 
        Password::from("secret123")
    )?;
    
    archive.push_archive_entry(
        SevenZArchiveEntry::from_path("sensitive.txt", "sensitive.txt")?
            .set_method(SevenZMethod::LZMA2)
    );
    
    archive.write()?;
    Ok(())
}

高级用法

自定义压缩参数

use sevenz_rust::{SevenZArchive, SevenZArchiveEntry, SevenZMethod, SevenZWriterOptions};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let options = SevenZWriterOptions {
        method: SevenZMethod::LZMA2,
        level: 9, // 压缩级别 1-9
        ..Default::default()
    };
    
    let mut archive = SevenZArchive::create_with_options("custom.7z", options)?;
    
    archive.push_archive_entry(
        SevenZArchiveEntry::from_path("large_file.bin", "large_file.bin")?
    );
    
    archive.write()?;
    Ok(())
}

处理大文件

use sevenz_rust::{SevenZArchive, SevenZArchiveEntry, SevenZMethod};
use std::fs::File;
use std::io::{self, BufReader};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut archive = SevenZArchive::create("large_files.7z")?;
    
    // 使用流式处理大文件
    let file = File::open("very_large_file.bin")?;
    let reader = BufReader::new(file);
    
    archive.push_archive_entry(
        SevenZArchiveEntry::from_reader(reader, "very_large_file.bin")?
            .set_method(SevenZMethod::LZMA2)
    );
    
    archive.write()?;
    Ok(())
}

完整示例

下面是一个完整的示例,演示如何压缩一个目录并设置密码保护:

use sevenz_rust::{SevenZArchive, SevenZArchiveEntry, SevenZMethod, Password};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建带密码的7z压缩文件
    let mut archive = SevenZArchive::create_with_password(
        "backup.7z", 
        Password::from("mysecurepassword")
    )?;
    
    // 添加单个文件
    archive.push_archive_entry(
        SevenZArchiveEntry::from_path(Path::new("important.txt"), "important.txt")?
            .set_method(SevenZMethod::LZMA2)
    );
    
    // 添加整个目录
    archive.push_source_dir(Path::new("data"), "backup_data")?;
    
    // 写入压缩文件
    archive.write()?;
    
    println!("压缩完成!");
    Ok(())
}

注意事项

  1. 对于非常大的文件,考虑使用流式处理以避免内存问题
  2. 密码保护的7z文件使用AES-256加密
  3. 目前不支持多卷压缩文件
  4. 解压进度可以通过实现Progress trait来监控

sevenz-rust库提供了简单而强大的接口来处理7z文件,适合需要纯Rust解决方案的项目。

回到顶部