use std::fs::File; #[allow(warnings)] use std::io::{BufWriter, Write}; use std::path::Path; use zip::write::FileOptions; use zip::ZipWriter; use std::io::Read; fn main() { // Path to the directory to compress let dir_path = Path::new("."); // Path to the output ZIP file let zip_path = Path::new("app.zip"); // Create a new ZIP file let file = File::create(&zip_path).unwrap(); let writer = BufWriter::new(file); let mut zip = ZipWriter::new(writer); // Recursively add all files and directories in the input directory to the ZIP file add_directory_to_zip(&mut zip, dir_path, "").unwrap(); // Finalize the ZIP file zip.finish().unwrap(); } // Recursively adds a directory (and all its contents) to a ZipWriter fn add_directory_to_zip(zip: &mut ZipWriter>, dir_path: &Path, base_path: &str) -> zip::result::ZipResult<()> { for entry in dir_path.read_dir()? { let path = entry?.path(); if path.is_dir() { // Recursively add the subdirectory let name = path.file_name().unwrap().to_str().unwrap(); let new_base_path = format!("{}/{}", base_path, name); add_directory_to_zip(zip, &path, &new_base_path)?; } else { // Add the file to the ZIP archive let name = path.file_name().unwrap().to_str().unwrap(); let options = FileOptions::default() .compression_method(zip::CompressionMethod::Deflated) .unix_permissions(0o755); // Set appropriate permissions for the file zip.start_file(format!("{}/{}", base_path, name), options)?; let mut file = File::open(&path)?; let mut buffer = Vec::new(); file.read_to_end(&mut buffer)?; zip.write_all(&buffer)?; } } Ok(()) }