diff --git a/Cargo.toml b/Cargo.toml index caf6a07f..5468919a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ keywords = ["zip", "archive"] description = """ Library to support the reading and writing of zip files. """ -edition = "2018" +edition = "2021" [dependencies] aes = { version = "0.7.5", optional = true } diff --git a/src/read.rs b/src/read.rs index ee22c940..81ca5264 100644 --- a/src/read.rs +++ b/src/read.rs @@ -13,7 +13,7 @@ use byteorder::{LittleEndian, ReadBytesExt}; use std::borrow::Cow; use std::collections::HashMap; use std::io::{self, prelude::*}; -use std::path::{Component, Path}; +use std::path::Path; use std::sync::Arc; #[cfg(any( @@ -906,20 +906,7 @@ impl<'a> ZipFile<'a> { /// to path-based exploits. It is recommended over /// [`ZipFile::mangled_name`]. pub fn enclosed_name(&self) -> Option<&Path> { - if self.data.file_name.contains('\0') { - return None; - } - let path = Path::new(&self.data.file_name); - let mut depth = 0usize; - for component in path.components() { - match component { - Component::Prefix(_) | Component::RootDir => return None, - Component::ParentDir => depth = depth.checked_sub(1)?, - Component::Normal(_) => depth += 1, - Component::CurDir => (), - } - } - Some(path) + self.data.enclosed_name() } /// Get the comment of the file diff --git a/src/types.rs b/src/types.rs index ad3a5700..68ae5258 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,5 @@ //! Types that specify what is contained in a ZIP. -#[cfg(feature = "time")] -use std::convert::{TryFrom, TryInto}; +use std::path; #[cfg(not(any( all(target_arch = "arm", target_pointer_width = "32"), target_arch = "mips", @@ -375,6 +374,23 @@ impl ZipFileData { }) } + pub fn enclosed_name(&self) -> Option<&path::Path> { + if self.file_name.contains('\0') { + return None; + } + let path = path::Path::new(&self.file_name); + let mut depth = 0usize; + for component in path.components() { + match component { + path::Component::Prefix(_) | path::Component::RootDir => return None, + path::Component::ParentDir => depth = depth.checked_sub(1)?, + path::Component::Normal(_) => depth += 1, + path::Component::CurDir => (), + } + } + Some(path) + } + pub fn zip64_extension(&self) -> bool { self.uncompressed_size > 0xFFFFFFFF || self.compressed_size > 0xFFFFFFFF