diff --git a/CHANGELOG.md b/CHANGELOG.md index 49ef92564..f4a9d5c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -273,5 +273,5 @@ ### Added - - `contains_file_named`: check whether a file entry exists in a zip file, without initializing the - metadata or needing to mutably borrow the `ZipArchive`. \ No newline at end of file + - `index_for_name`: get the index of a file given its name, without initializing metadata or needing to mutably borrow + the `ZipArchive`. \ No newline at end of file diff --git a/src/read.rs b/src/read.rs index 168923fcb..f9c924987 100644 --- a/src/read.rs +++ b/src/read.rs @@ -651,9 +651,10 @@ impl ZipArchive { self.by_name_with_optional_password(name, None) } - /// Check for a file entry, but do not decrypt it or initialize metadata. - pub fn contains_file_named(&self, name: &str) -> bool { - self.shared.names_map.contains_key(name) + /// Get the index of a file entry by name, if it's present. + #[inline(always)] + pub fn index_for_name(&self, name: &str) -> Option { + self.shared.names_map.get(name).map(|index_ref| *index_ref) } fn by_name_with_optional_password<'a>( @@ -661,11 +662,8 @@ impl ZipArchive { name: &str, password: Option<&[u8]>, ) -> ZipResult> { - let index = match self.shared.names_map.get(name) { - Some(index) => *index, - None => { - return Err(ZipError::FileNotFound); - } + let Some(index) = self.index_for_name(name) else { + return Err(ZipError::FileNotFound); }; self.by_index_with_optional_password(index, password) }