ZipExtractor

Struct ZipExtractor 

Source
pub struct ZipExtractor<R: ReadAt> { /* private fields */ }
Expand description

High-level ZIP file extractor.

This struct provides convenient methods for listing and extracting files from ZIP archives. It wraps the lower-level ZipParser and handles decompression automatically.

§Supported Compression Methods

  • STORED (0): No compression, data is copied directly
  • DEFLATE (8): Standard ZIP compression using flate2

§Generic Parameter

The extractor is generic over the reader type R, allowing it to work with both local files (LocalFileReader) and remote sources (HttpRangeReader).

Implementations§

Source§

impl<R: ReadAt> ZipExtractor<R>

Source

pub fn new(reader: Arc<R>) -> Self

Create a new extractor for the given reader.

§Arguments
  • reader - A shared reference to a reader implementing ReadAt
§Returns

A new extractor instance ready to list and extract files.

§Example
let reader = Arc::new(LocalFileReader::new(Path::new("archive.zip"))?);
let extractor = ZipExtractor::new(reader);
Source

pub async fn list_files(&self) -> Result<Vec<ZipFileEntry>>

List all files in the archive.

Returns metadata for all entries in the ZIP file, including both files and directories.

§Returns

A vector of ZipFileEntry with metadata for each entry.

§Errors

Returns an error if the archive is invalid or cannot be read.

§Example
for entry in extractor.list_files().await? {
    println!("{}: {} bytes", entry.file_name, entry.uncompressed_size);
}
Source

pub async fn extract_to_memory(&self, entry: &ZipFileEntry) -> Result<Vec<u8>>

Extract a file’s contents to memory.

Reads and decompresses the file data, returning it as a byte vector. This method handles both STORED and DEFLATE compression methods.

§Arguments
  • entry - The file entry to extract (from [list_files()])
§Returns

The decompressed file contents as a byte vector.

§Errors

Returns an error if:

  • The file uses an unsupported compression method
  • The data cannot be read or decompressed
§Memory Usage

This method loads the entire file into memory. For large files, consider using [extract_to_file()] instead.

§Example
let data = extractor.extract_to_memory(&entry).await?;
let text = String::from_utf8_lossy(&data);
println!("{}", text);
Source

pub async fn extract_to_file( &self, entry: &ZipFileEntry, output_path: &Path, ) -> Result<()>

Extract a file to the filesystem.

Reads, decompresses, and writes the file to the specified path. Parent directories are created automatically if they don’t exist.

§Arguments
  • entry - The file entry to extract
  • output_path - The filesystem path to write the file to
§Errors

Returns an error if:

  • The file cannot be read or decompressed
  • Parent directories cannot be created
  • The file cannot be written
§Example
extractor.extract_to_file(&entry, Path::new("output/file.txt")).await?;
Source

pub async fn extract_to_stdout(&self, entry: &ZipFileEntry) -> Result<()>

Extract a file’s contents to stdout.

Reads, decompresses, and writes the file directly to standard output. Useful for piping archive contents to other commands.

§Arguments
  • entry - The file entry to extract
§Errors

Returns an error if the file cannot be read, decompressed, or written.

§Example
// Equivalent to: runzip -p archive.zip file.txt | cat
extractor.extract_to_stdout(&entry).await?;

Auto Trait Implementations§

§

impl<R> Freeze for ZipExtractor<R>

§

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

§

impl<R> Send for ZipExtractor<R>

§

impl<R> Sync for ZipExtractor<R>

§

impl<R> Unpin for ZipExtractor<R>

§

impl<R> UnwindSafe for ZipExtractor<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more