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
use std::path::{Path, PathBuf};
use std::ffi::OsStr;
use zopfli::{self, Format, Options};
use file;
use error::*;
fn append(path: &Path, ext: &str) -> PathBuf {
let mut name = path.file_name().unwrap().to_os_string();
name.push(OsStr::new(ext));
path.with_file_name(name)
}
pub fn gz(data: &[u8], base_path: &Path) -> CDResult<PathBuf> {
let mut compressed = Vec::with_capacity(data.len() >> 1);
zopfli::compress(&Options::default(), &Format::Gzip, data, &mut compressed)?;
let full_path = append(base_path, ".gz");
file::put(&full_path, compressed)
.map_err(|why| CargoDebError::IoFile("unable to save compressed archive", why, full_path.clone()))?;
Ok(full_path)
}
#[cfg(feature = "lzma")]
pub fn xz_or_gz(data: &[u8], base_path: &Path) -> CDResult<PathBuf> {
use std::io::prelude::*;
use xz2::read::XzEncoder;
let mut compressed = Vec::with_capacity(data.len() >> 1);
let mut compressor = XzEncoder::new(data, 9);
compressor.read_to_end(&mut compressed)?;
let full_path = append(base_path, ".xz");
file::put(&full_path, &compressed)
.map_err(|why| CargoDebError::IoFile("unable to save compressed archive", why, full_path.clone()))?;
Ok(full_path)
}
#[cfg(not(feature = "lzma"))]
pub fn xz_or_gz(data: &[u8], base_path: &Path) -> CDResult<PathBuf> {
gz(data, base_path)
}