zip-extensions-rs
A companion crate to https://github.com/zip-rs/zip2 that offers high‑level utilities for everyday ZIP tasks—such as extracting archives to a directory, creating archives from folders, and supporting .zipignore and symlink preservation.
Usage examples
Configure dependencies
Add the following dependencies to the Cargo.toml file.
[]
= "6.0"
= "0.11.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 File;
use ZipArchiveExtensions;
...
let file = create?;
let mut archive = new?;
archive.extract?;
Alternatively, the zip_extract helper can be used.
use *;
...
let archive_file: PathBuf = ...
let target_dir: PathBuf = ...
zip_extract?;
Extracting an archive entry into memory
The zip_extract_file_to_memory method can be used to extract entries ad-hoc into memory.
use *;
let archive_file = from_str?;
let entry_path = from_str?;
let mut buffer : = vec!;
match zip_extract_file_to_memory ;
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 ZipWriter;
use ZipWriterExtensions;
...
let file = create?;
let zip = new;
zip.create_from_directory?;
Alternatively, the zip_create_from_directory helper can be used.
use *;
...
let archive_file: PathBuf = ...
let source_dir: PathBuf = ...
zip_create_from_directory?;
Creating an archive from a directory with preserved symlinks
Use create_from_directory_with_options together with the symlink-preserving helper if you want symbolic links to be stored as links instead of being resolved to their targets. This preserves the link metadata inside the ZIP.
use zip_create_from_directory_preserve_symlinks_with_options;
...
let archive_file: PathBuf = ...
let source_dir: PathBuf = ...
let opts = default.compression_method;
zip_create_from_directory_preserve_symlinks_with_options?;
Note: Preserving symlinks can be unsafe when the ZIP will be extracted by unknown tools that do not validate symlink targets. Prefer the non-preserving variant for general distribution.
Creating an archive from a directory while respecting .zipignore files
To exclude files and folders based on .zipignore rules, pass the ZipIgnoreEntryHandler together with create_from_directory_with_options on a ZipWriter.
use ZipWriter;
use ZipIgnoreEntryHandler;
use ZipWriterExtensions;
...
let source_dir: PathBuf = ...
let zip_writer = new;
let opts = default.compression_method;
zip_writer.create_from_directory_with_options?;
Place a .zipignore file in any directory you want to influence.