46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use std::{
|
|
env,
|
|
fs::File,
|
|
io::{Read, Write},
|
|
path::Path,
|
|
};
|
|
|
|
use ignore::Walk;
|
|
use zip::{CompressionMethod, write::SimpleFileOptions};
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let out_dir = env::var("OUT_DIR")?;
|
|
let src_dir = ".";
|
|
|
|
let zip_path = Path::new(&out_dir).join("archive.zip");
|
|
let zip_file = File::create(zip_path)?;
|
|
|
|
let walkdir = Walk::new(src_dir);
|
|
let it = walkdir.into_iter();
|
|
|
|
let mut zip = zip::ZipWriter::new(zip_file);
|
|
|
|
let options = SimpleFileOptions::default()
|
|
.compression_method(CompressionMethod::Deflated)
|
|
.unix_permissions(0o755);
|
|
|
|
for entry in it {
|
|
let entry = entry?;
|
|
let path = entry.path();
|
|
let name = path.strip_prefix(Path::new(src_dir))?;
|
|
|
|
if path.is_file() {
|
|
zip.start_file(name.to_str().unwrap(), options)?;
|
|
let mut f = File::open(path)?;
|
|
let mut buffer = Vec::new();
|
|
f.read_to_end(&mut buffer)?;
|
|
zip.write_all(&buffer)?;
|
|
} else if !name.as_os_str().is_empty() {
|
|
zip.add_directory(name.to_str().unwrap(), options)?;
|
|
}
|
|
}
|
|
zip.finish()?;
|
|
println!("cargo:rerun-if-changed={}", src_dir);
|
|
|
|
Ok(())
|
|
}
|