Skip to navigation
Zip and compress with rust
22.06.23
## Source code via mercurial * hg clone https://calantas.org/rustzip ## Cargo.toml ```toml [package] name = "rustzip" version = "0.1.0" edition = "2021" [dependencies] zip = { version = "0.6.6", default-features = false, features=["deflate"] } ``` ## src/main.rs ```rust use std::fs; use std::fs::File; use std::io::prelude::*; use std::mem::size_of; use zip::{write::FileOptions, CompressionMethod}; fn main() { println!("...test crete Zip file"); clean_zipped(); //write_binary_file(); write_text_file(); //write_bytes(); } fn write_binary_file() -> std::io::Result<()> { let path = std::path::Path::new("zipped/products.zip"); let file = std::fs::File::create(path).unwrap(); let mut zip = zip::ZipWriter::new(file); let options = FileOptions::default().compression_method(CompressionMethod::Deflated); zip.start_file("products.db", options); let fpath = std::path::Path::new("products.db"); let b = fs::read(fpath)?; zip.write_all(&b); zip.finish(); Ok(()) } fn write_text_file() -> std::io::Result<()> { let path = std::path::Path::new("zipped/lorum.zip"); let file = std::fs::File::create(path).unwrap(); let mut zip = zip::ZipWriter::new(file); let options = FileOptions::default().compression_method(CompressionMethod::Deflated); zip.start_file("lorum.txt", options); let fpath = std::path::Path::new("lorum.txt"); let b = fs::read(fpath)?; zip.write_all(&b); zip.finish(); Ok(()) } fn write_bytes() { let path = std::path::Path::new("zipped/lorum.zip"); let file = std::fs::File::create(path).unwrap(); let mut zip = zip::ZipWriter::new(file); let options = FileOptions::default().compression_method(CompressionMethod::Deflated); zip.start_file("lorum.txt", options); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.write(b"Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World"); zip.finish(); } fn clean_zipped() -> std::io::Result<()> { for path in fs::read_dir("zipped").unwrap() { let path = path.unwrap().path(); fs::remove_file(path).unwrap(); } Ok(()) } ```
https://docs.rs/zip/latest/zip/index.html
Reply
Anonymous
Information Epoch 1732427149
Clarity is better than cleverness.
Home
Notebook
Contact us