Crate decompress

Source
Expand description

Decompress an archive, supporting multiple format and stripping path prefixes

§Usage

You can use the default decompress stack:

#![allow(clippy::cognitive_complexity)]

use clap::{arg, command};
use decompress::ExtractOptsBuilder;

fn main() {
    let matches = command!()
        .arg(arg!(<archive> "Archive to extract"))
        .arg(arg!(<out> "Output folder"))
        .arg(arg!(
            -s --strip "Strip the first component of the archive"
        ))
        .get_matches();

    let archive = matches.get_one::<String>("archive").expect("required");
    let to = matches.get_one::<String>("out").expect("required");
    let strip = usize::from(matches.get_flag("strip"));
    let res = decompress::decompress(
        archive,
        to,
        &ExtractOptsBuilder::default().strip(strip).build().unwrap(),
    );
    println!("{res:?}");
}

Or build your own stack:

#![allow(clippy::cognitive_complexity)]

use clap::{arg, command};
use decompress::{decompressors, ExtractOptsBuilder};
use regex::Regex;

fn main() {
    let matches = command!()
        .arg(arg!(<archive> "Archive to Unzip (attempt any file)"))
        .arg(arg!(<out> "Output folder"))
        .arg(arg!(
            -s --strip "Strip the first component of the archive"
        ))
        .get_matches();

    let archive = matches.get_one::<String>("archive").expect("required");
    let to = matches.get_one::<String>("out").expect("required");
    let strip = usize::from(matches.get_flag("strip"));
    let decompressor = decompress::Decompress::build(vec![decompressors::zip::Zip::build(Some(
        Regex::new(r".*").unwrap(),
    ))]);

    let res = decompressor.decompress(
        archive,
        to,
        &ExtractOptsBuilder::default()
            .strip(strip)
            .filter(|path| {
                if let Some(path) = path.to_str() {
                    return path.ends_with("ex.sh");
                }
                false
            })
            .build()
            .unwrap(),
    );

    println!("{res:?}");
}

NOTE: to include or exclude decompressors types, use --features and disable default. This in turn removes or includes the (costly) dependencies these features need.

Modules§

decompressors

Structs§

Decompress
Represent a stack of decompressors with a default stack preconfigured when calling new
Decompression
ExtractOpts
ExtractOptsBuilder
Builder for ExtractOpts.
Listing

Enums§

DecompressError
ExtractOptsBuilderError
Error type for ExtractOptsBuilder

Traits§

Decompressor
Decompressor is a trait that you can implement to add your own decompressor type. A Decompressor is inserted into a stack, where given a potential archive file, many decompressors may attempt to test if they’re capable of unpacking it. The first Decompressor which will test true will be the one selected to unpack.

Functions§

can_decompress
Returns true if any of the decompressors in the stack can decompress this specific archive based on its path (no file opening)
can_decompress_content
Returns true if any of the decompressors in the stack can decompress this specific archive based on its content (reads first 8kb)
decompress
Decompress an archive with default decompressor set up
list
List an archive with default decompressor set up

Type Aliases§

FilterFn
MapFn