From a4e92ed9bec1f5879eb1c20dfe281c4d25ed5f89 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sun, 23 Jun 2024 15:19:40 +0900 Subject: Improve ZipFile --- src/zip/file.rs | 69 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 24 deletions(-) (limited to 'src/zip/file.rs') 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 @@ use crate::driver::ArchiveFile; -use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; +use crate::zip::{ZipError, ZipResult}; +use chrono::{DateTime, Local}; + +pub enum CompressionMethod { + Store, + Deflate, + BZIP2, + LZMA, + ZStd, + XZ, +} + +impl CompressionMethod { + pub(crate) fn from_struct_id(id: u16) -> ZipResult { + match id { + 0 => Ok(Self::Store), + 8 => Ok(Self::Deflate), + 12 => Ok(Self::BZIP2), + 14 => Ok(Self::LZMA), + 93 => Ok(Self::ZStd), + 95 => Ok(Self::XZ), + 1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => { + Err(ZipError::UnsupportedCompressionMethod.into()) + } + 21..=92 | 100.. => Err(ZipError::InvalidCompressionMethod.into()), + } + } +} pub struct ZipFile { - pub name: String, - pub datetime: NaiveDateTime, - pub compression_method: u16, + pub compression_method: CompressionMethod, + pub datetime: DateTime, + pub crc: u32, pub compressed_size: u64, pub size: u64, + pub header_pointer: u64, + pub name: String, pub comment: String, } -impl ArchiveFile for ZipFile {} - impl ZipFile { - pub(crate) fn new( - name: String, - dos_date: u16, - dos_time: u16, - compression_method: u16, + pub fn new( + compression_method: CompressionMethod, + datetime: DateTime, + crc: u32, compressed_size: u64, size: u64, + header_pointer: u64, + name: String, comment: String, ) -> Self { - let year = (dos_date >> 9 & 0x7F) + 1980; - let month = dos_date >> 5 & 0xF; - let day = dos_date & 0x1F; - - let hour = (dos_time >> 11) & 0x1F; - let minute = (dos_time >> 5) & 0x3F; - let seconds = (dos_time & 0x1F) * 2; - Self { - name, - datetime: NaiveDateTime::new( - NaiveDate::from_ymd_opt(year as i32, month as u32, day as u32).unwrap(), - NaiveTime::from_hms_opt(hour as u32, minute as u32, seconds as u32).unwrap(), - ), compression_method, + datetime, + crc, compressed_size, size, + header_pointer, + name, comment, } } } + +impl ArchiveFile for ZipFile {} -- cgit v1.2.3