diff options
Diffstat (limited to 'src/zip/file.rs')
| -rw-r--r-- | src/zip/file.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/zip/file.rs b/src/zip/file.rs index f735d65..b4f2d7c 100644 --- a/src/zip/file.rs +++ b/src/zip/file.rs | |||
| @@ -28,8 +28,100 @@ impl CompressionMethod { | |||
| 28 | } | 28 | } |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | pub struct BitFlag { | ||
| 32 | pub flag: u16, | ||
| 33 | } | ||
| 34 | |||
| 35 | pub mod bit { | ||
| 36 | #[derive(Debug, PartialEq, Eq)] | ||
| 37 | pub enum DeflateMode { | ||
| 38 | Normal, | ||
| 39 | Maximum, | ||
| 40 | Fast, | ||
| 41 | SuperFast, | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | macro_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 | |||
| 59 | impl 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 | |||
| 31 | pub struct ZipFile { | 122 | pub struct ZipFile { |
| 32 | pub compression_method: CompressionMethod, | 123 | pub compression_method: CompressionMethod, |
| 124 | pub bit_flag: BitFlag, | ||
| 33 | pub datetime: DateTime<Local>, | 125 | pub datetime: DateTime<Local>, |
| 34 | pub crc: u32, | 126 | pub crc: u32, |
| 35 | pub compressed_size: u64, | 127 | pub compressed_size: u64, |
| @@ -42,6 +134,7 @@ pub struct ZipFile { | |||
| 42 | impl ZipFile { | 134 | impl ZipFile { |
| 43 | pub fn new( | 135 | pub fn new( |
| 44 | compression_method: CompressionMethod, | 136 | compression_method: CompressionMethod, |
| 137 | bit_flag: BitFlag, | ||
| 45 | datetime: DateTime<Local>, | 138 | datetime: DateTime<Local>, |
| 46 | crc: u32, | 139 | crc: u32, |
| 47 | compressed_size: u64, | 140 | compressed_size: u64, |
| @@ -52,6 +145,7 @@ impl ZipFile { | |||
| 52 | ) -> Self { | 145 | ) -> Self { |
| 53 | Self { | 146 | Self { |
| 54 | compression_method, | 147 | compression_method, |
| 148 | bit_flag, | ||
| 55 | datetime, | 149 | datetime, |
| 56 | crc, | 150 | crc, |
| 57 | compressed_size, | 151 | compressed_size, |
