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<()>
impl ZipArchive<()>
Sourcepub fn with_max_search_space(max_search_space: u64) -> ZipLocator
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).
Sourcepub fn from_slice<T: AsRef<[u8]>>(data: T) -> Result<ZipSliceArchive<T>, Error>
pub fn from_slice<T: AsRef<[u8]>>(data: T) -> Result<ZipSliceArchive<T>, Error>
Parses an archive from in-memory data.
Sourcepub fn from_file(
file: File,
buffer: &mut [u8],
) -> Result<ZipArchive<FileReader>, Error>
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.
Sourcepub fn from_seekable<R>(
reader: R,
buffer: &mut [u8],
) -> Result<ZipArchive<MutexReader<R>>, Error>
pub fn from_seekable<R>( reader: R, buffer: &mut [u8], ) -> Result<ZipArchive<MutexReader<R>>, Error>
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>
impl<R> ZipArchive<R>
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consumes this archive and returns the underlying reader.
Sourcepub fn entries<'archive, 'buf>(
&'archive self,
buffer: &'buf mut [u8],
) -> ZipEntries<'archive, 'buf, R>
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(())
}Sourcepub fn entries_hint(&self) -> u64
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.
Sourcepub fn comment(&self) -> RangeReader<&R> ⓘ
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);Sourcepub fn eocd_offset(&self) -> u64
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);Sourcepub fn directory_offset(&self) -> u64
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.
Sourcepub fn end_offset(&self) -> u64
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,
impl<R> ZipArchive<R>where
R: ReaderAt,
Trait Implementations§
Source§impl<R: Clone> Clone for ZipArchive<R>
impl<R: Clone> Clone for ZipArchive<R>
Source§fn clone(&self) -> ZipArchive<R>
fn clone(&self) -> ZipArchive<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more