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
use std::{
io::{Read, Seek},
path::Path,
};
use zip::{read::ZipFile, result::ZipResult};
pub struct ArtifactFile<'a>(ZipFile<'a>);
impl ArtifactFile<'_> {
pub fn name(&self) -> &str {
self.0.name()
}
pub fn path(&self) -> Option<&Path> {
self.0.enclosed_name()
}
}
impl Read for ArtifactFile<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.read(buf)
}
}
pub(crate) trait ReadSeek: Read + Seek {}
impl<T> ReadSeek for T where T: Read + Seek {}
pub struct Artifact {
zip: zip::ZipArchive<Box<dyn ReadSeek + Send>>,
}
impl Artifact {
pub(crate) fn new(data: Box<dyn ReadSeek + Send>) -> ZipResult<Self> {
let zip = zip::ZipArchive::new(data)?;
Ok(Self { zip })
}
pub fn file_names(&self) -> impl Iterator<Item = &str> {
self.zip.file_names()
}
pub fn file(&mut self, name: &str) -> Option<ArtifactFile> {
self.zip.by_name(name).ok().map(ArtifactFile)
}
pub fn by_index(&mut self, i: usize) -> Option<ArtifactFile> {
self.zip.by_index(i).ok().map(ArtifactFile)
}
pub fn is_empty(&self) -> bool {
self.zip.is_empty()
}
pub fn len(&self) -> usize {
self.zip.len()
}
}