diff options
| author | Igor Tolmachev <me@igorek.dev> | 2024-06-23 15:19:40 +0900 |
|---|---|---|
| committer | Igor Tolmachev <me@igorek.dev> | 2024-06-23 15:34:35 +0900 |
| commit | a4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89 (patch) | |
| tree | 6acef99bfaf57c573b543f29836701a92c215a83 /src/zip/file.rs | |
| parent | 62aaae347d87c5c9411f1e9f8db525b7c2c603d2 (diff) | |
| download | archivator-a4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89.tar.gz archivator-a4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89.zip | |
Improve ZipFile
Diffstat (limited to 'src/zip/file.rs')
| -rw-r--r-- | src/zip/file.rs | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/src/zip/file.rs b/src/zip/file.rs index d5b3327..f735d65 100644 --- a/src/zip/file.rs +++ b/src/zip/file.rs | |||
| @@ -1,45 +1,66 @@ | |||
| 1 | use crate::driver::ArchiveFile; | 1 | use crate::driver::ArchiveFile; |
| 2 | use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; | 2 | use crate::zip::{ZipError, ZipResult}; |
| 3 | use chrono::{DateTime, Local}; | ||
| 4 | |||
| 5 | pub enum CompressionMethod { | ||
| 6 | Store, | ||
| 7 | Deflate, | ||
| 8 | BZIP2, | ||
| 9 | LZMA, | ||
| 10 | ZStd, | ||
| 11 | XZ, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl 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 | 93 => Ok(Self::ZStd), | ||
| 22 | 95 => Ok(Self::XZ), | ||
| 23 | 1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => { | ||
| 24 | Err(ZipError::UnsupportedCompressionMethod.into()) | ||
| 25 | } | ||
| 26 | 21..=92 | 100.. => Err(ZipError::InvalidCompressionMethod.into()), | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 3 | 30 | ||
| 4 | pub struct ZipFile { | 31 | pub struct ZipFile { |
| 5 | pub name: String, | 32 | pub compression_method: CompressionMethod, |
| 6 | pub datetime: NaiveDateTime, | 33 | pub datetime: DateTime<Local>, |
| 7 | pub compression_method: u16, | 34 | pub crc: u32, |
| 8 | pub compressed_size: u64, | 35 | pub compressed_size: u64, |
| 9 | pub size: u64, | 36 | pub size: u64, |
| 37 | pub header_pointer: u64, | ||
| 38 | pub name: String, | ||
| 10 | pub comment: String, | 39 | pub comment: String, |
| 11 | } | 40 | } |
| 12 | 41 | ||
| 13 | impl ArchiveFile for ZipFile {} | ||
| 14 | |||
| 15 | impl ZipFile { | 42 | impl ZipFile { |
| 16 | pub(crate) fn new( | 43 | pub fn new( |
| 17 | name: String, | 44 | compression_method: CompressionMethod, |
| 18 | dos_date: u16, | 45 | datetime: DateTime<Local>, |
| 19 | dos_time: u16, | 46 | crc: u32, |
| 20 | compression_method: u16, | ||
| 21 | compressed_size: u64, | 47 | compressed_size: u64, |
| 22 | size: u64, | 48 | size: u64, |
| 49 | header_pointer: u64, | ||
| 50 | name: String, | ||
| 23 | comment: String, | 51 | comment: String, |
| 24 | ) -> Self { | 52 | ) -> Self { |
| 25 | let year = (dos_date >> 9 & 0x7F) + 1980; | ||
| 26 | let month = dos_date >> 5 & 0xF; | ||
| 27 | let day = dos_date & 0x1F; | ||
| 28 | |||
| 29 | let hour = (dos_time >> 11) & 0x1F; | ||
| 30 | let minute = (dos_time >> 5) & 0x3F; | ||
| 31 | let seconds = (dos_time & 0x1F) * 2; | ||
| 32 | |||
| 33 | Self { | 53 | Self { |
| 34 | name, | ||
| 35 | datetime: NaiveDateTime::new( | ||
| 36 | NaiveDate::from_ymd_opt(year as i32, month as u32, day as u32).unwrap(), | ||
| 37 | NaiveTime::from_hms_opt(hour as u32, minute as u32, seconds as u32).unwrap(), | ||
| 38 | ), | ||
| 39 | compression_method, | 54 | compression_method, |
| 55 | datetime, | ||
| 56 | crc, | ||
| 40 | compressed_size, | 57 | compressed_size, |
| 41 | size, | 58 | size, |
| 59 | header_pointer, | ||
| 60 | name, | ||
| 42 | comment, | 61 | comment, |
| 43 | } | 62 | } |
| 44 | } | 63 | } |
| 45 | } | 64 | } |
| 65 | |||
| 66 | impl ArchiveFile for ZipFile {} | ||
