ZipArchive

Struct ZipArchive 

Source
pub struct ZipArchive<R> { /* private fields */ }
Expand description

The main entrypoint for reading a Zip archive.

It can be created from a slice, a file, or any Read + Seek source.

§Examples

Creating from a file:

fn example_from_file(file: File) -> Result<(), Error> {
    let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
    let archive = ZipArchive::from_file(file, &mut buffer)?;
    Ok(())
}

For more complex use cases, use the ZipLocator to locate an archive.

Implementations§

Source§

impl ZipArchive<()>

Source

pub fn with_max_search_space(max_search_space: u64) -> ZipLocator

Creates a ZipLocator configured with a maximum search space for the End of Central Directory Record (EOCD).

Source

pub fn from_slice<T: AsRef<[u8]>>(data: T) -> Result<ZipSliceArchive<T>, Error>

Parses an archive from in-memory data.

Source

pub fn from_file( file: File, buffer: &mut [u8], ) -> Result<ZipArchive<FileReader>, Error>

Parses an archive from a file by reading the End of Central Directory.

A buffer is required to read parts of the file. RECOMMENDED_BUFFER_SIZE can be used to construct this buffer.

Source

pub fn from_seekable<R>( reader: R, buffer: &mut [u8], ) -> Result<ZipArchive<MutexReader<R>>, Error>
where R: Read + Seek,

Parses an archive from a seekable reader.

Prefer ZipArchive::from_file and ZipArchive::from_slice when possible, as they are more efficient due to not wrapping the underlying reader in a mutex to support positioned io.

fn example(zip_data: &[u8]) -> Result<(), Error> {
    let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
    let archive = ZipArchive::from_seekable(Cursor::new(zip_data), &mut buffer)?;
    Ok(())
}
Source§

impl<R> ZipArchive<R>

Source

pub fn get_ref(&self) -> &R

Returns a reference to the underlying reader.

Source

pub fn into_inner(self) -> R

Consumes this archive and returns the underlying reader.

Source

pub fn entries<'archive, 'buf>( &'archive self, buffer: &'buf mut [u8], ) -> ZipEntries<'archive, 'buf, R>

Returns a lending iterator over the entries in the central directory of the archive.

Requires a mutable buffer to read directory entries from the underlying reader.

fn example(file: File) -> Result<(), Error> {
    let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
    let archive = ZipArchive::from_file(file, &mut buffer)?;
    let entries_hint = archive.entries_hint();
    let mut actual_entries = 0;
    let mut entries_iterator = archive.entries(&mut buffer);
    while let Some(_) = entries_iterator.next_entry()? {
        actual_entries += 1;
    }
    println!("Found {} entries (hint: {})", actual_entries, entries_hint);
    Ok(())
}
Source

pub fn entries_hint(&self) -> u64

Returns a hint for the total number of entries in the archive.

This value is read from the End of Central Directory record.

Source

pub fn comment(&self) -> RangeReader<&R>

Returns a Read implementation for the comment of the zip archive.

Use RangeReader::remaining() to get the comment length before reading. It is guaranteed to be less than u16::MAX.

§Examples
use rawzip::{ZipArchive, ZipStr, RECOMMENDED_BUFFER_SIZE};
use std::io::Read;
use std::fs::File;

let file = File::open("assets/test.zip")?;
let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let archive = ZipArchive::from_file(file, &mut buffer)?;

let mut comment_reader = archive.comment();
let comment_len = comment_reader.remaining() as usize;
comment_reader.read_exact(&mut buffer[..comment_len])?;

let actual = ZipStr::new(&buffer[..comment_len]);
let expected = ZipStr::new(b"This is a zipfile comment.");
assert_eq!(expected, actual);
Source

pub fn eocd_offset(&self) -> u64

Returns the offset of the End of Central Directory (EOCD) signature.

This is the byte position where the EOCD signature (0x06054b50) was found. Useful for recovery scenarios when dealing with false EOCD signatures or when restarting archive searches from a known position.

§Examples
let archive = ZipArchive::from_file(file, &mut buffer)?;
let eocd_position = archive.eocd_offset();

let locator = ZipLocator::new();
let reader = archive.get_ref();
let maybe_previous = locator.locate_in_reader(reader, &mut buffer, eocd_position);
Source

pub fn directory_offset(&self) -> u64

The declared offset of the start of the central directory.

To verify the validity of this offset, start iterating through the central directory via entries(). Ensure no errors are returned on the first entry.

This value is useful when calculating the amount of prelude data exists in the data, as it will serve as the upper bound until each file’s ZipFileHeaderRecord::local_header_offset can be examined.

Source

pub fn end_offset(&self) -> u64

Returns the offset where the ZIP archive ends.

This returns the position immediately after the last byte of the ZIP archive, including the End of Central Directory record and any comment. This is useful for extracting trailing data.

The calculation does not rely on any self reported values from the archive.

This can be used in conjunction with the starting offset calculation start offset as shown in RangeReader to determine the exact byte range (and thus size) of the ZIP archive within a context of a larger file.

Source§

impl<R> ZipArchive<R>
where R: ReaderAt,

Source

pub fn get_entry( &self, entry: ZipArchiveEntryWayfinder, ) -> Result<ZipEntry<'_, R>, Error>

Seeks to the given file entry in the zip archive.

Trait Implementations§

Source§

impl<R: Clone> Clone for ZipArchive<R>

Source§

fn clone(&self) -> ZipArchive<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<R: Debug> Debug for ZipArchive<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for ZipArchive<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for ZipArchive<R>
where R: RefUnwindSafe,

§

impl<R> Send for ZipArchive<R>
where R: Send,

§

impl<R> Sync for ZipArchive<R>
where R: Sync,

§

impl<R> Unpin for ZipArchive<R>
where R: Unpin,

§

impl<R> UnwindSafe for ZipArchive<R>
where R: UnwindSafe,

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.