use crate::format::*;
pub struct Archive {
pub(crate) size: u64,
pub(crate) encoding: Encoding,
pub(crate) entries: Vec<StoredEntry>,
pub(crate) comment: Option<String>,
}
impl Archive {
pub fn size(&self) -> u64 {
self.size
}
pub fn entries(&self) -> impl Iterator<Item = &StoredEntry> {
self.entries.iter()
}
pub fn by_name<N: AsRef<str>>(&self, name: N) -> Option<&StoredEntry> {
self.entries.iter().find(|&x| x.name() == name.as_ref())
}
pub fn encoding(&self) -> Encoding {
self.encoding
}
pub fn comment(&self) -> Option<&String> {
self.comment.as_ref()
}
}
#[derive(Clone)]
pub struct Entry {
pub name: String,
pub method: Method,
pub comment: Option<String>,
pub modified: chrono::DateTime<chrono::offset::Utc>,
pub created: Option<chrono::DateTime<chrono::offset::Utc>>,
pub accessed: Option<chrono::DateTime<chrono::offset::Utc>>,
}
#[derive(Clone)]
pub struct StoredEntry {
pub entry: Entry,
pub crc32: u32,
pub header_offset: u64,
pub compressed_size: u64,
pub uncompressed_size: u64,
pub external_attrs: u32,
pub creator_version: Version,
pub reader_version: Version,
pub flags: u16,
pub uid: Option<u32>,
pub gid: Option<u32>,
pub mode: Mode,
pub extra_fields: Vec<ExtraField>,
pub is_zip64: bool,
}
impl StoredEntry {
pub fn name(&self) -> &str {
self.entry.name.as_ref()
}
pub fn comment(&self) -> Option<&str> {
self.entry.comment.as_ref().map(|x| x.as_ref())
}
pub fn method(&self) -> Method {
self.entry.method
}
pub fn modified(&self) -> DateTime<Utc> {
self.entry.modified
}
pub fn created(&self) -> Option<&DateTime<Utc>> {
self.entry.created.as_ref()
}
pub fn accessed(&self) -> Option<&DateTime<Utc>> {
self.entry.accessed.as_ref()
}
}
#[derive(Debug)]
pub enum EntryContents {
Directory,
File,
Symlink,
}
impl StoredEntry {
pub fn contents(&self) -> EntryContents {
if self.mode.has(Mode::SYMLINK) {
EntryContents::Symlink
} else if self.mode.has(Mode::DIR) {
EntryContents::Directory
} else {
EntryContents::File
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Method {
Store,
Deflate,
Bzip2,
Lzma,
Unsupported(u16),
}
impl From<u16> for Method {
fn from(m: u16) -> Self {
use Method::*;
match m {
0 => Store,
8 => Deflate,
12 => Bzip2,
14 => Lzma,
_ => Unsupported(m),
}
}
}
impl From<Method> for u16 {
fn from(m: Method) -> Self {
use Method::*;
match m {
Store => 0,
Deflate => 8,
Bzip2 => 12,
Lzma => 14,
Unsupported(m) => m,
}
}
}