From 51694e1f0b2730915e0a57ec6d8de503cf06ef9a Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Thu, 27 Jun 2024 16:15:00 +0900 Subject: Create file driver and implement file reader --- src/zip/driver.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/zip/driver.rs') diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 650344e..0db845d 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -1,27 +1,27 @@ use crate::driver::{ArchiveRead, ArchiveWrite, Driver}; -use crate::zip::file::{BitFlag, CompressionMethod}; use crate::zip::structs::{deserialize, EOCDR64Locator, ExtraHeader, CDR, EOCDR, EOCDR64}; -use crate::zip::{ZipError, ZipFile, ZipResult}; +use crate::zip::{BitFlag, CompressionMethod, ZipError, ZipFile, ZipFileInfo, ZipResult}; use chrono::{Local, NaiveDate, NaiveDateTime, NaiveTime}; use std::collections::HashMap as Map; use std::io::{Read, Seek, SeekFrom, Write}; -pub struct Zip { - io: IO, +pub struct Zip { + io: Io, - files: Map, + files: Map, comment: String, } -impl Driver for Zip { +impl Driver for Zip { type Error = ZipError; - type IO = IO; - type File = ZipFile; + type Io = Io; + type FileDriver<'d> = ZipFile<'d, Self::Io> where Self::Io: 'd; + type FileInfo = ZipFileInfo; } -impl ArchiveRead for Zip { - fn read(mut io: Self::IO) -> ZipResult { +impl ArchiveRead for Zip { + fn read(mut io: Self::Io) -> ZipResult { // Search eocdr let limit = 65557.min(io.seek(SeekFrom::End(0))?) as i64; let start = io.seek(SeekFrom::End(-limit))?; @@ -137,7 +137,7 @@ impl ArchiveRead for Zip { files.insert( name.clone(), - ZipFile::new( + ZipFileInfo::new( CompressionMethod::from_struct_id(cdr.compression_method)?, BitFlag::new(cdr.bit_flag), NaiveDateTime::new( @@ -169,23 +169,27 @@ impl ArchiveRead for Zip { Ok(Self { io, files, comment }) } - fn files(&self) -> Vec<&Self::File> { - let mut files: Vec<&Self::File> = self.files.values().collect(); + fn files(&self) -> Vec<&Self::FileInfo> { + let mut files: Vec<&Self::FileInfo> = self.files.values().collect(); files.sort_by_key(|f| &f.name); files } - fn get_file(&self, name: &str) -> Option<&Self::File> { + fn get_file_info(&self, name: &str) -> Option<&Self::FileInfo> { self.files.get(name) } + + fn get_file_reader<'d>(&'d mut self, name: &str) -> Option> { + Some(ZipFile::new(&mut self.io, self.files.get(name)?).unwrap()) + } } -impl Zip { +impl Zip { pub fn comment(&self) -> &String { &self.comment } } -impl ArchiveWrite for Zip {} +impl ArchiveWrite for Zip {} -impl Zip {} +impl Zip {} -- cgit v1.2.3