-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
80 lines (66 loc) · 2.13 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// use zip;
use std::io::prelude::*;
use std::io::{Seek, Write};
use std::iter::Iterator;
use zip::result::ZipError;
use zip::write::FileOptions;
use std::fs::{self, File};
use std::path::Path;
use walkdir::{DirEntry, WalkDir};
const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored);
const SRC: &str = "resources";
const DST: &str = "resources.zip";
fn main() {
match make_zip(SRC, DST, METHOD_STORED.unwrap()) {
Ok(_) => println!("build-resources={}={}", SRC, DST),
Err(e) => panic!("build-resources=error={:?}", e),
}
}
fn zip_dir<T>(
items: &mut dyn Iterator<Item = DirEntry>,
prefix: &str,
writer: T,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<()>
where
T: Write + Seek,
{
let mut zip_writer = zip::ZipWriter::new(writer);
let options = FileOptions::default()
.compression_method(method)
.unix_permissions(0o755);
let mut buffer = Vec::new();
for entry in items {
let path = entry.path();
let name = path.strip_prefix(Path::new(prefix)).unwrap();
if path.is_file() {
println!("build-resources={:?}={:?}", path, name);
zip_writer.start_file(String::from(name.to_string_lossy()), options)?;
let mut f = File::open(path)?;
f.read_to_end(&mut buffer)?;
zip_writer.write_all(&*buffer)?;
buffer.clear();
} else if !name.as_os_str().is_empty() {
println!("build-resources={:?}={:?}", path, name);
zip_writer.start_file(String::from(name.to_string_lossy()), options)?;
}
}
zip_writer.finish()?;
Result::Ok(())
}
fn make_zip(
src_dir: &str,
dst_file: &str,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<()> {
if !Path::new(src_dir).is_dir() {
return Err(ZipError::FileNotFound);
}
let path = Path::new(dst_file);
fs::remove_file(&path).ok();
let file = File::create(&path).unwrap();
let walkdir = WalkDir::new(src_dir);
let it = walkdir.into_iter();
zip_dir(&mut it.filter_map(|e| e.ok()), src_dir, file, method)?;
Ok(())
}