This project is a Rust-based command-line utility for compressing files into a ZIP archive and extracting files from a ZIP archive. It demonstrates how to use the zip crate for handling ZIP files in Rust.
- Zip Files: Compress multiple files and directories into a ZIP archive.
- Unzip Files: Extract files from an existing ZIP archive.
- Handles Nested Directories: Automatically includes subdirectories during compression.
- Cross-Platform: Compatible with Linux, macOS, and Windows.
- Rust installed on your machine.
- Clone this repository:
git clone https://github.com/Signor1/rust_file_zip_and_unzip.git cd rust_file_zip_and_unzip
- Build the project:
cargo build --release
To extract files from a ZIP archive:
cargo run -- unzip <filename>
Example:
cargo run -- unzip hello_cargo.zip
This will extract the contents of hello_cargo.zip
into the current directory.
To create a ZIP archive from files and directories:
cargo run -- zip <output_zipfile> <files_to_zip>...
Example:
cargo run -- zip my_archive.zip file1.txt folder1/
This will create a ZIP file named my_archive.zip
containing file1.txt
and the contents of folder1/
.
src/main.rs
: Contains the main logic for zipping and unzipping files.- Dependencies: Uses the zip crate for handling ZIP files.
- Opens the specified ZIP file.
- Iterates through the archive contents.
- Extracts files and directories, preserving structure.
- Optionally restores file permissions (on Unix-based systems).
- Creates a new ZIP archive.
- Adds specified files to the archive.
- Recursively adds contents of directories to the archive.
Given the following directory structure:
work/
hello_cargo.zip
file1.txt
folder1/
file2.txt
- To extract
hello_cargo.zip
:cargo run -- unzip hello_cargo.zip
- To create a new archive:
cargo run -- zip new_archive.zip file1.txt folder1/
Contributions are welcome! Feel free to open issues or submit pull requests.