aboutsummaryrefslogtreecommitdiff
path: root/src/zip/file.rs
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-06-23 15:19:40 +0900
committerIgor Tolmachev <me@igorek.dev>2024-06-23 15:34:35 +0900
commita4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89 (patch)
tree6acef99bfaf57c573b543f29836701a92c215a83 /src/zip/file.rs
parent62aaae347d87c5c9411f1e9f8db525b7c2c603d2 (diff)
downloadarchivator-a4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89.tar.gz
archivator-a4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89.zip
Improve ZipFile
Diffstat (limited to 'src/zip/file.rs')
-rw-r--r--src/zip/file.rs69
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 @@
1use crate::driver::ArchiveFile; 1use crate::driver::ArchiveFile;
2use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; 2use crate::zip::{ZipError, ZipResult};
3use chrono::{DateTime, Local};
4
5pub enum CompressionMethod {
6 Store,
7 Deflate,
8 BZIP2,
9 LZMA,
10 ZStd,
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 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
4pub struct ZipFile { 31pub 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
13impl ArchiveFile for ZipFile {}
14
15impl ZipFile { 42impl 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
66impl ArchiveFile for ZipFile {}