add code download route
This commit is contained in:
parent
480e16d070
commit
dddc4b4081
6 changed files with 131 additions and 2 deletions
46
build.rs
Normal file
46
build.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
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(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue