rust-lang / flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
https://docs.rs/flate2
Apache License 2.0
900 stars 162 forks source link

build tar file from Folder without to save it. #264

Closed Mozartuss closed 3 years ago

Mozartuss commented 3 years ago

Is it possible to build a tar-file recursively from a Folder without to save it. I currently use the Builder with .append_dir_all(). In my use case I only need the byte vector of the tar-file to work with this.

Mozartuss commented 3 years ago

If someone is looking for the solution here it is:

the function compress all files in a given folder path and return a Vec<u8>

fn compress(input_path: &PathBuf) -> anyhow::Result<Vec<u8>, io::Error> {
    let encoder = GzEncoder::new(Vec::new(), Compression::fast());
    let mut tar = tar::Builder::new(encoder);
    tar.append_dir_all(".", &input_path)?;
    let encoder_data: GzEncoder<Vec<u8>> = tar.into_inner()?;
    let compress_vec: Vec<u8> = encoder_data.finish()?;
    Ok(compress_vec)
}