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
use std::path::Path;
use tar::{Builder, Archive};
use walkdir::WalkDir;
use libflate::gzip::{Encoder, Decoder};
use std::io::{Write, Read};
pub fn compress(archive: &Vec<u8>) -> std::io::Result<Vec<u8>> {
let mut encoder = Encoder::new(Vec::new())?;
println!("encoder created, data len: {}", archive.len());
println!("write: {}", encoder.write(&archive)?);
println!("data written");
encoder.finish().into_result()
}
pub fn decompress(encoded_data: &[u8]) -> std::io::Result<Vec<u8>>{
let mut decoder = Decoder::new(encoded_data)?;
let mut decoded_data = Vec::new();
decoder.read_to_end(&mut decoded_data)?;
Ok(decoded_data)
}
pub fn archive(root: &str) -> std::io::Result<Vec<u8>> {
let archive = Vec::new();
let mut builder = Builder::new(archive);
let results: Vec<std::io::Result<()>> = WalkDir::new(root)
.into_iter()
.map(|p| p.unwrap().into_path())
.filter(|p| p.is_file())
.map(|p| builder.append_path(p))
.collect();
for result in results.into_iter() {
result?
}
builder.into_inner()
}
pub fn dearchive<P: AsRef<Path>>(archive_data: &[u8], root: P) -> std::io::Result<()> {
let mut archive = Archive::new(archive_data);
for entry in archive.entries()? {
let mut entry = entry?;
entry.unpack(format!("{}{}", root.as_ref().display(), entry.path()?.display()))?;
}
Ok(())
}
pub fn download() {}