zip Library for Rust: Complete Guide to Working with ZIP Archives
In modern Rust development, working with archives is a common task. Whether it's packaging logs, unpacking configurations, or creating backups, the standard zip library (crate zip) is the de facto solution for reading and writing ZIP files. In this article, we will thoroughly cover how to install the library, create an archive, extract files, and also explore advanced techniques: working with passwords, streams, and large data.
Installation and Setup
Add the dependency to Cargo.toml:
[dependencies]zip = "2.1"After that, import the necessary modules in your code:
use std::fs::File;use std::io::{Read, Write, BufReader, BufWriter};use zip::ZipWriter;use zip::write::FileOptions;use zip::read::ZipArchive;Creating a ZIP Archive
To create a new archive, use ZipWriter. Let's look at a basic example — packing two text files:
use std::fs::File;use std::io::Write;use zip::ZipWriter;use zip::write::FileOptions;
fn create_archive() -> zip::result::ZipResult<()> { let file = File::create("example.zip")?; let mut zip = ZipWriter::new(file);
// Add the first file zip.start_file("hello.txt", FileOptions::default())?; zip.write_all(b"Hello, Rust archive!")?;
// Add the second file with compression let options = FileOptions::default() .compression_method(zip::CompressionMethod::Deflated) .unix_permissions(0o755); zip.start_file("data/config.json", options)?; zip.write_all(b"{\\"version\\": 1}")?;
zip.finish()?; Ok(())}Important: The start_file method opens a new file inside the archive. Options allow you to set the compression method (default is Stored — no compression, Deflated — standard ZIP compression).
Reading and Extracting an Archive
For reading, use ZipArchive. Below is a function that lists files and extracts them to the current directory:
use std::fs::File;use std::io::{Read, BufReader};use zip::ZipArchive;
fn extract_archive() -> zip::result::ZipResult<()> { let file = File::open("example.zip")?; let reader = BufReader::new(file); let mut archive = ZipArchive::new(reader)?;
for i in 0..archive.len() { let mut entry = archive.by_index(i)?; let name = entry.name().to_string(); println!("Extracting: {}", name);
// Create a file on disk let mut output = File::create(&name)?; std::io::copy(&mut entry, &mut output)?; } Ok(())}The by_index method returns an iterator over entries. If you need access by name, use by_name("path/to/file.txt"). Note: names in ZIP may contain paths with the / separator — this is normal for cross-platform compatibility.
Working with Passwords (Encryption)
The zip library supports password-based encryption using the AES algorithm (default AES-256). To create an encrypted archive:
use zip::ZipWriter;use zip::write::FileOptions;use zip::AesMode;
fn create_encrypted_archive() -> zip::result::ZipResult<()> { let file = File::create("secret.zip")?; let mut zip = ZipWriter::new(file);
let options = FileOptions::default() .compression_method(zip::CompressionMethod::Deflated) .with_aes_encryption(AesMode::Aes256, "my_strong_password");
zip.start_file("secret.txt", options)?; zip.write_all(b"Top secret data")?; zip.finish()?; Ok(())}To read an encrypted archive, pass the password to the by_index_decrypt or by_n
Also in library
Crypto-js for JavaScript (Node.js): Complete Guide with Examples
OpenAPI / Swagger for Java: Complete Guide with Examples
Email Sending in TypeScript: A Complete Guide with nodemailer and SMTP
Cryptography in Kotlin: Encryption and Hashing