12 releases

0.6.2 Sep 3, 2023
0.6.1 Jul 29, 2021
0.6.0 Nov 30, 2020
0.5.0 Jul 24, 2020
0.1.4 Mar 19, 2020

#34 in Compression

Download history 1581/week @ 2024-01-02 2414/week @ 2024-01-09 2416/week @ 2024-01-16 2468/week @ 2024-01-23 3005/week @ 2024-01-30 2401/week @ 2024-02-06 3041/week @ 2024-02-13 2315/week @ 2024-02-20 2643/week @ 2024-02-27 2221/week @ 2024-03-05 2377/week @ 2024-03-12 2534/week @ 2024-03-19 2208/week @ 2024-03-26 2541/week @ 2024-04-02 2287/week @ 2024-04-09 2230/week @ 2024-04-16

9,672 downloads per month
Used in 33 crates (22 directly)

MIT license

16KB
285 lines

zip-extensions-rs

Rust Build status Crates.io

An extension crate for https://github.com/mvdnes/zip-rs that provides high-level functions for common ZIP tasks, such as extracting archives to a directory.

Usage examples

Configure dependencies

Add the following dependencies to the Cargo.toml file.

[dependencies]
zip = "0.6"
zip-extensions = "0.6"

See https://github.com/mvdnes/zip-rs fur further information about zip dependencies.

Extracting an archive to a directory

The ZipArchiveExtensions trait provides the extract method that can be used to unzip an archive to a directory.

use std::fs::File;
use zip_extensions::read::ZipArchiveExtensions;
...

let file = File::create(archive_file)?;
let mut archive = zip::ZipArchive::new(file)?;
archive.extract(&target_path)?;

Alternatively, the zip_extract helper can be used.

use zip_extensions::*;
...
let archive_file: PathBuf = ...
let target_dir: PathBuf = ...
zip_extract(&archive_file, &target_dir)?;

Extracting an archive entry into memory

The zip_extract_file_to_memory method can be used to extract entries ad-hoc into memory.

use zip_extensions::*;

let archive_file = PathBuf::from_str(r#"Baloo_Da_2.zip"#)?;
let entry_path = PathBuf::from_str("BalooDa2-Medium.ttf")?;

let mut buffer : Vec<u8> = vec![];
match zip_extract_file_to_memory(&archive_file, &entry_path, &mut buffer) {
    Ok(()) => { println!("Extracted {} bytes from archive.", buffer.len()) },
    Err(e) => { println!("The entry does not exist.") }
};

Creating an archive from a directory

The ZipWriterExtensions trait provides the create_from_directory and create_from_directory_with_options methods that can be used to add an entire directory hierarchy to an archive.

use zip::ZipWriter;
use zip_extensions::write::ZipWriterExtensions;
...

let file = File::create(archive_file)?;
let mut zip = ZipWriter::new(file);
zip.create_from_directory(&source_path)?;

Alternatively, the zip_create_from_directory helper can be used.

use zip_extensions::*;
...
let archive_file: PathBuf = ...
let source_dir: PathBuf = ...
zip_create_from_directory(&archive_file, &source_dir)?;

Dependencies

~5MB
~80K SLoC