-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-zip.rs
90 lines (77 loc) · 2.32 KB
/
build-zip.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
81
82
83
84
85
86
87
88
89
90
use std::{fs::File, io::Read, io::Seek, io::Write, path::Path, path::PathBuf};
use walkdir::WalkDir;
use zip::{result::ZipError, write::FileOptions};
fn zip_dir<T>(
it: &mut dyn Iterator<Item = walkdir::DirEntry>,
prefix: &str,
writer: T,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<()>
where
T: Write + Seek,
{
let mut zip = zip::ZipWriter::new(writer);
let options = FileOptions::default()
.compression_method(method)
.unix_permissions(0o755);
let mut buffer = Vec::new();
for entry in it {
let path = entry.path();
let name = path.strip_prefix(Path::new(prefix)).unwrap();
// Write file or directory explicitly
// Some unzip tools unzip files with directory paths correctly, some do not!
if path.is_file() {
println!("adding file {:?} as {:?} ...", path, name);
zip.start_file(name.to_string_lossy(), options)?;
let mut f = File::open(path)?;
f.read_to_end(&mut buffer)?;
zip.write_all(&*buffer)?;
buffer.clear();
} else if !name.as_os_str().is_empty() {
// Only if not root! Avoids path spec / warning
// and mapname conversion failed error on unzip
println!("adding dir {:?} as {:?} ...", path, name);
zip.add_directory(name.to_string_lossy(), options)?;
}
}
zip.finish()?;
Result::Ok(())
}
fn doit(
src: &PathBuf,
path: &PathBuf,
method: zip::CompressionMethod,
) -> zip::result::ZipResult<()> {
if !src.is_dir() {
return Err(ZipError::FileNotFound);
}
let file = File::create(&path).unwrap();
let walkdir = WalkDir::new(src);
let it = walkdir.into_iter();
zip_dir(
&mut it.filter_map(|e| e.ok()),
src.to_str().unwrap(),
file,
method,
)?;
Ok(())
}
fn main() {
println!("cargo:rerun-if-changed=resources/*");
doit(
&Path::new("resources").to_path_buf(),
&Path::new("resources.zip").to_path_buf(),
zip::CompressionMethod::Stored,
)
.unwrap();
/*fs_extra::dir::copy(
"resources",
out_dir,
&fs_extra::dir::CopyOptions {
overwrite: true,
copy_inside: true,
..Default::default()
},
)
.unwrap();*/
}