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
use std::fs::{self, File};
use std::io::{self, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::thread;
use self::zip_rs::write::FileOptions;
use zip as zip_rs;
pub fn create_zip(dst_file: &File, src_dir: &Path, files: Vec<PathBuf>) -> io::Result<()> {
let mut writer = {
let zip = zip_rs::ZipWriter::new(dst_file);
BufWriter::new(zip)
};
let options = FileOptions::default()
.compression_method(zip_rs::CompressionMethod::Stored)
.unix_permissions(0o644);
for x in &files {
let file_path = src_dir.join(x);
if let Ok(file) = File::open(file_path.clone()) {
info!("compress: {:?} -> {:?}", file_path, x);
writer.get_mut().start_file_from_path(x, options)?;
io::copy(&mut BufReader::new(file), &mut writer)?;
writer.flush()?;
}
}
writer.get_mut().finish()?;
dst_file.sync_all()?;
Ok(())
}
pub fn extract_files(from_archive: File, dest: &Path, files: Vec<PathBuf>) -> io::Result<()> {
let dest: PathBuf = PathBuf::from(dest);
let files: Vec<_> = files.to_vec();
let res = thread::spawn(move || {
let mut archive = zip_rs::ZipArchive::new(from_archive).expect("archive file exists");
for x in files {
if let Ok(file) = archive.by_name(x.to_str().expect("valid path")) {
let path = dest.join(file.sanitized_name());
let parent_dir = path.parent().expect("valid parent dir");
fs::create_dir_all(&parent_dir).expect("create parent dir");
let outfile = fs::File::create(&path).expect("file created");
io::copy(&mut BufReader::new(file), &mut BufWriter::new(outfile))
.expect("write to file");
info!("extract_files: {:?} -> {:?}", x, path);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = PermissionsExt::from_mode(0o644);
fs::set_permissions(&path, mode).expect("set file permissions");
}
}
}
})
.join();
res.map_err(|e| {
error!("failed to extract files from zip: {:?}", e);
io::Error::new(io::ErrorKind::Other, "failed to extract files from zip")
})
}