From 6444bee8f3e188be014841ea8cd7cfb53eb03ed9 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sat, 15 Jun 2024 13:09:12 +0900 Subject: Optimize file reading --- src/zip/driver.rs | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 733d44b..313bf8d 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -4,7 +4,7 @@ use crate::zip::structs::{EOCDR64Locator, CDR, EOCDR, EOCDR64}; use crate::zip::ZipFile; use std::collections::HashMap as Map; use std::fs::File; -use std::io::{Read, Seek, SeekFrom, Write}; +use std::io::{Cursor, Read, Seek, SeekFrom, Write}; pub struct Zip { io: IO, @@ -89,34 +89,22 @@ impl ArchiveRead for Zip { io.read(&mut buf)?; buf }; - let mut records = buf.as_slice(); + let mut p: usize = 0; for _ in 0..cd_records { - let buf = { - let mut buf = [0; 46]; - records.read(&mut buf)?; - buf - }; - - if u32::from_le_bytes(buf[0..4].try_into().unwrap()) != 0x02014b50 { + if u32::from_le_bytes(buf[p..p + 4].try_into().unwrap()) != 0x02014b50 { return Err(ZipError::InvalidCDRSignature.into()); } - let cdr: CDR = bincode::deserialize(&buf[4..]).map_err(|_| ZipError::InvalidCDR)?; - let name = { - let mut buf = vec![0; cdr.name_len as usize]; - records.read(&mut buf)?; - String::from_utf8(buf).map_err(|_| ZipError::InvalidFileName)? - }; - let extra_fields = { - let mut buf = vec![0; cdr.extra_field_len as usize]; - records.read(&mut buf)?; - buf - }; - let comment = { - let mut buf = vec![0; cdr.comment_len as usize]; - records.read(&mut buf)?; - String::from_utf8(buf).map_err(|_| ZipError::InvalidFileComment)? - }; + p += 4; + let cdr: CDR = + bincode::deserialize(&buf[p..p + 42]).map_err(|_| ZipError::InvalidCDR)?; + p += 42; + let name = String::from_utf8(buf[p..p + cdr.name_len as usize].into()).unwrap(); + p += cdr.name_len as usize; + let extra_fields: Vec = buf[p..p + cdr.extra_field_len as usize].into(); + p += cdr.extra_field_len as usize; + let comment = String::from_utf8(buf[p..p + cdr.comment_len as usize].into()).unwrap(); + p += cdr.comment_len as usize; files.insert( name.clone(), -- cgit v1.2.3