15 unstable releases (7 breaking)

0.8.1 Jul 26, 2024
0.7.0 May 31, 2024
0.6.2 Sep 3, 2023
0.6.1 Jul 29, 2021
0.4.0 Mar 25, 2020

#22 in Compression

Download history 3157/week @ 2024-07-21 3345/week @ 2024-07-28 3399/week @ 2024-08-04 3663/week @ 2024-08-11 3826/week @ 2024-08-18 3662/week @ 2024-08-25 3581/week @ 2024-09-01 3634/week @ 2024-09-08 3304/week @ 2024-09-15 3673/week @ 2024-09-22 4233/week @ 2024-09-29 4123/week @ 2024-10-06 3998/week @ 2024-10-13 3997/week @ 2024-10-20 3957/week @ 2024-10-27 3645/week @ 2024-11-03

15,910 downloads per month
Used in 40 crates (28 directly)

MIT license

18KB
297 lines

zip-extensions-rs

Rust Crates.io

An extension crate for https://github.com/zip-rs/zip2 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 = "2.1.1"
zip-extensions = "0.8.0"

See https://github.com/zip-rs/zip2 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 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

~1.6–4MB
~69K SLoC