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 directlyDEFLATE(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>
impl<R: ReadAt> ZipExtractor<R>
Sourcepub fn new(reader: Arc<R>) -> Self
pub fn new(reader: Arc<R>) -> Self
Create a new extractor for the given reader.
§Arguments
reader- A shared reference to a reader implementingReadAt
§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);Sourcepub async fn list_files(&self) -> Result<Vec<ZipFileEntry>>
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);
}Sourcepub async fn extract_to_memory(&self, entry: &ZipFileEntry) -> Result<Vec<u8>>
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);Sourcepub async fn extract_to_file(
&self,
entry: &ZipFileEntry,
output_path: &Path,
) -> Result<()>
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 extractoutput_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?;Sourcepub async fn extract_to_stdout(&self, entry: &ZipFileEntry) -> Result<()>
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?;