aboutsummaryrefslogtreecommitdiff
path: root/src/zip/file/info.rs
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-07-01 19:12:40 +0900
committerIgor Tolmachev <me@igorek.dev>2024-07-02 18:59:00 +0900
commit5d3d32ded672b67471d9d7c85ebbe691129cc51c (patch)
tree1f9a82196d69cfec34af595a659e4d74a80b0c92 /src/zip/file/info.rs
parent6d5f8f046b3b24e50cb1a0e7751c6bc9170ed9d1 (diff)
downloadarchivator-5d3d32ded672b67471d9d7c85ebbe691129cc51c.tar.gz
archivator-5d3d32ded672b67471d9d7c85ebbe691129cc51c.zip
Add compression support (lzma and xz are broken)
Diffstat (limited to 'src/zip/file/info.rs')
-rw-r--r--src/zip/file/info.rs161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/zip/file/info.rs b/src/zip/file/info.rs
new file mode 100644
index 0000000..2891b59
--- /dev/null
+++ b/src/zip/file/info.rs
@@ -0,0 +1,161 @@
1use crate::driver::ArchiveFileInfo;
2use crate::zip::{ZipError, ZipResult};
3use chrono::{DateTime, Local};
4
5#[derive(Debug, Clone)]
6pub enum CompressionMethod {
7 Store,
8 Deflate,
9 BZip2,
10 LZMA,
11 XZ,
12}
13
14impl CompressionMethod {
15 pub(crate) fn from_struct_id(id: u16) -> ZipResult<Self> {
16 match id {
17 0 => Ok(Self::Store),
18 8 => Ok(Self::Deflate),
19 12 => Ok(Self::BZip2),
20 14 => Ok(Self::LZMA),
21 95 => Ok(Self::XZ),
22 1..=7 | 9..=11 | 13 | 15..=20 | 93..=94 | 96..=99 => {
23 Err(ZipError::UnsupportedCompressionMethod.into())
24 }
25 21..=92 | 100.. => Err(ZipError::InvalidCompressionMethod.into()),
26 }
27 }
28}
29
30#[derive(Debug, Clone)]
31pub struct BitFlag {
32 flag: u16,
33}
34
35pub mod bit {
36 #[derive(Debug, PartialEq, Eq)]
37 pub enum DeflateMode {
38 Normal,
39 Maximum,
40 Fast,
41 SuperFast,
42 }
43}
44
45macro_rules! get_set_bit_flag {
46 {$($get:ident $set:ident $bit:expr)+} => {
47 $(
48 pub fn $get(&self) -> bool {
49 self.get_bit($bit)
50 }
51
52 pub fn $set(&mut self, enable: bool) {
53 self.set_bit($bit, enable);
54 }
55 )*
56 };
57}
58
59impl BitFlag {
60 pub fn new(flag: u16) -> Self {
61 Self { flag }
62 }
63
64 #[inline]
65 fn get_bit(&self, bit: u32) -> bool {
66 (self.flag & 2u16.pow(bit)) > 0
67 }
68
69 #[inline]
70 fn set_bit(&mut self, bit: u32, enable: bool) {
71 if enable {
72 self.flag |= 2u16.pow(bit);
73 } else {
74 self.flag &= !2u16.pow(bit);
75 }
76 }
77
78 pub fn deflate_mode(&self) -> bit::DeflateMode {
79 match self.flag & 6 {
80 0 => bit::DeflateMode::Normal,
81 2 => bit::DeflateMode::Maximum,
82 4 => bit::DeflateMode::Fast,
83 6 => bit::DeflateMode::SuperFast,
84 _ => panic!("impossible"),
85 }
86 }
87
88 pub fn set_deflate_mode(&mut self, mode: bit::DeflateMode) {
89 match mode {
90 bit::DeflateMode::Normal => {
91 self.set_bit(1, false);
92 self.set_bit(2, false);
93 }
94 bit::DeflateMode::Maximum => {
95 self.set_bit(1, true);
96 self.set_bit(2, false);
97 }
98 bit::DeflateMode::Fast => {
99 self.set_bit(1, false);
100 self.set_bit(2, true);
101 }
102 bit::DeflateMode::SuperFast => {
103 self.set_bit(1, true);
104 self.set_bit(2, true);
105 }
106 }
107 }
108
109 get_set_bit_flag! {
110 is_encrypted set_encrypted 0
111 is_imploding_8k set_imploding_8k 1
112 is_imploding_3sf_trees set_imploding_3sf_trees 2
113 is_lzma_has_eos_marker set_lzma_has_eos_marker 1
114 is_has_data_descriptor set_has_data_descriptor 3
115 is_patched_data set_patched_data 5
116 is_strong_encryption set_strong_encryption 6
117 is_utf8 set_utf8 11
118 is_cd_encryption set_cd_encryption 13
119 }
120}
121
122#[derive(Debug, Clone)]
123pub struct ZipFileInfo {
124 pub compression_method: CompressionMethod,
125 pub bit_flag: BitFlag,
126 pub datetime: DateTime<Local>,
127 pub crc: u32,
128 pub compressed_size: u64,
129 pub size: u64,
130 pub header_pointer: u64,
131 pub name: String,
132 pub comment: String,
133}
134
135impl ZipFileInfo {
136 pub fn new(
137 compression_method: CompressionMethod,
138 bit_flag: BitFlag,
139 datetime: DateTime<Local>,
140 crc: u32,
141 compressed_size: u64,
142 size: u64,
143 header_pointer: u64,
144 name: String,
145 comment: String,
146 ) -> Self {
147 Self {
148 compression_method,
149 bit_flag,
150 datetime,
151 crc,
152 compressed_size,
153 size,
154 header_pointer,
155 name,
156 comment,
157 }
158 }
159}
160
161impl ArchiveFileInfo for ZipFileInfo {}