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 flate2::write::GzEncoder;
use crate::storage::{
IntoInner,
PathGenerator,
StorageBackend,
TarArchive,
};
type InnerObject = GzEncoder<std::io::BufWriter<std::fs::File>>;
pub struct TarGzFile<G: PathGenerator> {
archive: TarArchive<InnerObject, G>,
}
impl<G: PathGenerator> TarGzFile<G> {
pub fn new<P: AsRef<std::path::Path>>(
target_path: P,
path_generator: G,
compression_level: Option<flate2::Compression>,
) -> std::io::Result<Self> {
let archive = std::fs::File::create(target_path)?;
let archive = std::io::BufWriter::new(archive);
let archive = flate2::write::GzEncoder::new(
archive,
match compression_level {
Some(level) => level,
None => flate2::Compression::default(),
},
);
Ok(Self {
archive: TarArchive::new(archive, path_generator),
})
}
}
impl<G: PathGenerator> StorageBackend for TarGzFile<G> {
type Error = <TarArchive<InnerObject, G> as StorageBackend>::Error;
fn append_file(&mut self, mfile: libatm::MIDIFile, mode: Option<u32>) -> Result<(), Self::Error> {
self.archive.append_file(mfile, mode)
}
fn finish(&mut self) -> Result<(), Self::Error> {
self.archive.finish()
}
}
impl<G: PathGenerator> IntoInner for TarGzFile<G> {
type Inner = InnerObject;
fn into_inner(self) -> Result<Self::Inner, <Self as StorageBackend>::Error> {
self.archive.into_inner()
}
}