From 73518b1f83a3fbacd1f6ad38e606bb755f557665 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Tue, 5 May 2026 16:37:47 -0400 Subject: [PATCH] Add 'get file metadata' function --- hubtools/src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/hubtools/src/lib.rs b/hubtools/src/lib.rs index d57d890..c242042 100644 --- a/hubtools/src/lib.rs +++ b/hubtools/src/lib.rs @@ -531,6 +531,15 @@ pub struct RawHubrisArchive { pub archive_version: u32, } +/// Metadata for a file within a Hubris archive +#[derive(Debug)] +pub struct FileMetadata { + /// File name + pub name: String, + /// Uncompressed size + pub size: u64, +} + impl RawHubrisArchive { pub fn from_vec(contents: Vec) -> Result { Self::new(contents, ArchiveSource::Memory) @@ -638,6 +647,25 @@ impl RawHubrisArchive { Ok(buffer) } + /// Returns file metadata in the ZIP archive by index + /// + /// This function does not decompress the file, so it's cheaper than + /// [`extract_file_by_index`](Self::extract_file_by_index). + pub fn file_metadata_by_index( + &self, + index: usize, + ) -> Result { + let cursor = Cursor::new(self.zip.as_slice()); + let mut archive = + zip::ZipArchive::new(cursor).map_err(Error::ZipNewError)?; + let file = archive + .by_index(index) + .map_err(|e| Error::BadFileIndex(e, index))?; + let name = file.name().to_string(); + let size = file.size(); + Ok(FileMetadata { name, size }) + } + /// Extacts a file from the ZIP archive by index /// /// Returns a tuple of `(file name, contents)` or an error