Git Product home page Git Product logo

zip-extensions-rs's Introduction

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)?;

zip-extensions-rs's People

Contributors

cosmichorrordev avatar matzefriedrich avatar rtsuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zip-extensions-rs's Issues

`FileOptions` should be mappable

First of all, thank you so much for this awesome extension.

I'm currently having trouble setting permissions to just a few of the files I'm adding to an archive.

What if the functionality were to be like this?

    let mut zip = ZipWriter::new(file);

    zip.create_from_directory_with_options(&PathBuf::from(&source_dir), |file: &PathBuf| {
        if file.eq(&PathBuf::from("dir/my_executable")) {
            FileOptions::default().unix_permissions(0o775)
        } else {
            FileOptions::default()
        }
    })

This allows changing the FileOptions per item inside the source directory. The file argument in the closure can be entry_path from the directory_entry_iterator.

I'd love to implement this but it would obviously be a breaking change. There's also the option of dedicating a whole new function for this. ๐Ÿค”

Zip dependency upgrade

Any plans for a new release with up-to-date dependencies, i.e. using zip 0.6 instead of 0.5?

Include target directory name in path hierarchy

Hello.

Crate provides very convenient function create_from_directory, but how to include target folder name in zip hierarchy?

For example, i have path c:\media with folders: dvd1, dvd2, dvdN.

I want to create zip for dvd1 with paths:

  • dvd1\some\file.jpg
  • dvd1\some\sub2\fish.jpg

Is is it possible?

p. s: Thanks for your crate it really helps :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.