ZipEntry

Struct ZipEntry 

Source
pub struct ZipEntry<'archive, R> { /* private fields */ }
Expand description

Represents a single entry (file or directory) within a ZipArchive

Implementations§

Source§

impl<'archive, R> ZipEntry<'archive, R>
where R: ReaderAt,

Source

pub fn reader(&self) -> ZipReader<&'archive R>

Returns a ZipReader for reading the compressed data of this entry.

Source

pub fn verifying_reader<D>(&self, reader: D) -> ZipVerifier<D, &'archive R>
where D: Read,

Returns a reader that wraps a decompressor and verify the size and CRC of the decompressed data once finished.

Source

pub fn compressed_data_range(&self) -> (u64, u64)

Returns a tuple of start and end byte offsets for the compressed data within the underlying reader.

This method uses the information from the local file header in its calculations.

§Security Usage

This method is useful for detecting overlapping entries, which are often used in zip bombs. By comparing the ranges returned by this method across multiple entries, you can identify when entries share compressed data:

let archive = ZipArchive::from_slice(data)?;
let mut ranges = Vec::new();

for entry_result in archive.entries() {
    let entry = entry_result?;
    let wayfinder = entry.wayfinder();
    if let Ok(zip_entry) = archive.get_entry(wayfinder) {
        ranges.push(zip_entry.compressed_data_range());
    }
}

// Check for overlapping ranges
ranges.sort_by_key(|&(start, _)| start);
for window in ranges.windows(2) {
    let (_, end1) = window[0];
    let (start2, _) = window[1];
    if end1 > start2 {
        panic!("Warning: Overlapping entries detected!");
    }
}
Source

pub fn local_header<'a>( &self, buffer: &'a mut [u8], ) -> Result<ZipLocalFileHeader<'a>, Error>

Returns the local file header information.

This method reads the local file header to which may differ from the central directory data. Most ZIP tools use the central directory as authoritative, but access to local header data can be useful:

The local header may contain:

  • Additional or different extra fields (richer timestamp data, etc.)
  • Different filename than the central directory (security concern)

The buffer argument must be large enough to hold both the filename and extra fields from the local header or a too small error will be returned.

§Examples
// Test with filename mismatch test fixture
let file = File::open("assets/filename_mismatch_test.zip")?;
let mut buf = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let archive = ZipArchive::from_file(file, &mut buf)?;

let mut entries = archive.entries(&mut buf);
let entry_header = entries.next_entry()?.unwrap();

// Central directory shows one filename
assert_eq!(entry_header.file_path().as_ref(), b"malware.exe");
let wayfinder = entry_header.wayfinder();
let entry = archive.get_entry(wayfinder)?;

// Read the local header
let mut local_buffer = vec![0u8; 1024];
let local_header = entry.local_header(&mut local_buffer)?;

// Local header shows different filename
assert_eq!(local_header.file_path().as_ref(), b"safe_file.txt");

// Access extra fields from local header
let mut found_fields = 0;
for (field_id, _data) in local_header.extra_fields() {
    found_fields += 1;
    // Could check for specific extra field types here
    println!("Found extra field: {:04x}", field_id.as_u16());
}

Trait Implementations§

Source§

impl<'archive, R: Clone> Clone for ZipEntry<'archive, R>

Source§

fn clone(&self) -> ZipEntry<'archive, R>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'archive, R: Debug> Debug for ZipEntry<'archive, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'archive, R> Freeze for ZipEntry<'archive, R>

§

impl<'archive, R> RefUnwindSafe for ZipEntry<'archive, R>
where R: RefUnwindSafe,

§

impl<'archive, R> Send for ZipEntry<'archive, R>
where R: Sync,

§

impl<'archive, R> Sync for ZipEntry<'archive, R>
where R: Sync,

§

impl<'archive, R> Unpin for ZipEntry<'archive, R>

§

impl<'archive, R> UnwindSafe for ZipEntry<'archive, R>
where R: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.