Struct ZipArchive

struct ZipArchive<R> { ... }

ZIP archive reader

At the moment, this type is cheap to clone if this is the case for the reader it uses. However, this is not guaranteed by this crate and it may change in the future.

use std::io::prelude::*;
fn list_zip_contents(reader: impl Read + Seek) -> zip::result::ZipResult<()> {
    use zip::HasZipMetadata;
    let mut zip = zip::ZipArchive::new(reader)?;

    for i in 0..zip.len() {
        let mut file = zip.by_index(i)?;
        println!("Filename: {}", file.name());
        std::io::copy(&mut file, &mut std::io::stdout())?;
    }

    Ok(())
}

Implementations

impl<R> ZipArchive<R>

fn decompressed_size(self: &Self) -> Option<u128>

Total size of the files in the archive, if it can be known. Doesn't include directories or metadata.

impl<R: Read + Seek> ZipArchive<R>

fn get_aes_verification_key_and_salt(self: &mut Self, file_number: usize) -> ZipResult<Option<AesInfo>>

Returns the verification value and salt for the AES encryption of the file

It fails if the file number is invalid.

Returns

  • None if the file is not encrypted with AES
fn new(reader: R) -> ZipResult<ZipArchive<R>>

Read a ZIP archive, collecting the files it contains.

This uses the central directory record of the ZIP file, and ignores local file headers.

A default Config is used.

fn with_config(config: Config, reader: R) -> ZipResult<ZipArchive<R>>

Read a ZIP archive providing a read configuration, collecting the files it contains.

This uses the central directory record of the ZIP file, and ignores local file headers.

fn extract<P: AsRef<Path>>(self: &mut Self, directory: P) -> ZipResult<()>

Extract a Zip archive into a directory, overwriting files if they already exist. Paths are sanitized with ZipFile::enclosed_name. Symbolic links are only created and followed if the target is within the destination directory (this is checked conservatively using std::fs::canonicalize).

Extraction is not atomic. If an error is encountered, some of the files may be left on disk. However, on Unix targets, no newly-created directories with part but not all of their contents extracted will be readable, writable or usable as process working directories by any non-root user except you.

On Unix and Windows, symbolic links are extracted correctly. On other platforms such as WebAssembly, symbolic links aren't supported, so they're extracted as normal files containing the target path in UTF-8.

fn extract_unwrapped_root_dir<P: AsRef<Path>, impl RootDirFilter: RootDirFilter>(self: &mut Self, directory: P, root_dir_filter: impl RootDirFilter) -> ZipResult<()>

Extracts a Zip archive into a directory in the same fashion as ZipArchive::extract, but detects a "root" directory in the archive (a single top-level directory that contains the rest of the archive's entries) and extracts its contents directly.

For a sensible default filter, you can use root_dir_common_filter. For a custom filter, see RootDirFilter.

See ZipArchive::root_dir for more information on how the root directory is detected and the meaning of the filter parameter.

Example

Imagine a Zip archive with the following structure:

root/file1.txt
root/file2.txt
root/sub/file3.txt
root/sub/subsub/file4.txt

If the archive is extracted to foo using ZipArchive::extract, the resulting directory structure will be:

foo/root/file1.txt
foo/root/file2.txt
foo/root/sub/file3.txt
foo/root/sub/subsub/file4.txt

If the archive is extracted to foo using ZipArchive::extract_unwrapped_root_dir, the resulting directory structure will be:

foo/file1.txt
foo/file2.txt
foo/sub/file3.txt
foo/sub/subsub/file4.txt

Example - No Root Directory

Imagine a Zip archive with the following structure:

root/file1.txt
root/file2.txt
root/sub/file3.txt
root/sub/subsub/file4.txt
other/file5.txt

Due to the presence of the other directory, ZipArchive::extract_unwrapped_root_dir will extract this in the same fashion as ZipArchive::extract as there is now no "root directory."

fn len(self: &Self) -> usize

Number of files contained in this zip.

fn central_directory_start(self: &Self) -> u64

Get the starting offset of the zip central directory.

fn is_empty(self: &Self) -> bool

Whether this zip archive contains no files

fn offset(self: &Self) -> u64

Get the offset from the beginning of the underlying reader that this zip begins at, in bytes.

Normally this value is zero, but if the zip has arbitrary data prepended to it, then this value will be the size of that prepended data.

fn comment(self: &Self) -> &[u8]

Get the comment of the zip archive.

fn zip64_comment(self: &Self) -> Option<&[u8]>

Get the ZIP64 comment of the zip archive, if it is ZIP64.

fn file_names(self: &Self) -> impl Iterator<Item = &str>

Returns an iterator over all the file and directory names in this archive.

fn has_overlapping_files(self: &mut Self) -> ZipResult<bool>

Returns Ok(true) if any compressed data in this archive belongs to more than one file. This doesn't make the archive invalid, but some programs will refuse to decompress it because the copies would take up space independently in the destination.

fn by_name_decrypt(self: &mut Self, name: &str, password: &[u8]) -> ZipResult<ZipFile<'_, R>>

Search for a file entry by name, decrypt with given password

Warning

The implementation of the cryptographic algorithms has not gone through a correctness review, and you should assume it is insecure: passwords used with this API may be compromised.

This function sometimes accepts wrong password. This is because the ZIP spec only allows us to check for a 1/256 chance that the password is correct. There are many passwords out there that will also pass the validity checks we are able to perform. This is a weakness of the ZipCrypto algorithm, due to its fairly primitive approach to cryptography.

fn by_name(self: &mut Self, name: &str) -> ZipResult<ZipFile<'_, R>>

Search for a file entry by name

fn index_for_name(self: &Self, name: &str) -> Option<usize>

Get the index of a file entry by name, if it's present.

fn by_path_decrypt<T: AsRef<Path>>(self: &mut Self, path: T, password: &[u8]) -> ZipResult<ZipFile<'_, R>>

Search for a file entry by path, decrypt with given password

Warning

The implementation of the cryptographic algorithms has not gone through a correctness review, and you should assume it is insecure: passwords used with this API may be compromised.

This function sometimes accepts wrong password. This is because the ZIP spec only allows us to check for a 1/256 chance that the password is correct. There are many passwords out there that will also pass the validity checks we are able to perform. This is a weakness of the ZipCrypto algorithm, due to its fairly primitive approach to cryptography.

fn by_path<T: AsRef<Path>>(self: &mut Self, path: T) -> ZipResult<ZipFile<'_, R>>

Search for a file entry by path

fn index_for_path<T: AsRef<Path>>(self: &Self, path: T) -> Option<usize>

Get the index of a file entry by path, if it's present.

fn name_for_index(self: &Self, index: usize) -> Option<&str>

Get the name of a file entry, if it's present.

fn by_name_seek(self: &mut Self, name: &str) -> ZipResult<ZipFileSeek<'_, R>>

Search for a file entry by name and return a seekable object.

fn by_index_seek(self: &mut Self, index: usize) -> ZipResult<ZipFileSeek<'_, R>>

Search for a file entry by index and return a seekable object.

fn by_index_decrypt(self: &mut Self, file_number: usize, password: &[u8]) -> ZipResult<ZipFile<'_, R>>

Get a contained file by index, decrypt with given password

Warning

The implementation of the cryptographic algorithms has not gone through a correctness review, and you should assume it is insecure: passwords used with this API may be compromised.

This function sometimes accepts wrong password. This is because the ZIP spec only allows us to check for a 1/256 chance that the password is correct. There are many passwords out there that will also pass the validity checks we are able to perform. This is a weakness of the ZipCrypto algorithm, due to its fairly primitive approach to cryptography.

fn by_index(self: &mut Self, file_number: usize) -> ZipResult<ZipFile<'_, R>>

Get a contained file by index

fn by_index_raw(self: &mut Self, file_number: usize) -> ZipResult<ZipFile<'_, R>>

Get a contained file by index without decompressing it

fn by_index_with_options(self: &mut Self, file_number: usize, options: ZipReadOptions<'_>) -> ZipResult<ZipFile<'_, R>>

Get a contained file by index with options.

fn root_dir<impl RootDirFilter: RootDirFilter>(self: &Self, filter: impl RootDirFilter) -> ZipResult<Option<PathBuf>>

Find the "root directory" of an archive if it exists, filtering out irrelevant entries when searching.

Our definition of a "root directory" is a single top-level directory that contains the rest of the archive's entries. This is useful for extracting archives that contain a single top-level directory that you want to "unwrap" and extract directly.

For a sensible default filter, you can use root_dir_common_filter. For a custom filter, see RootDirFilter.

fn into_inner(self: Self) -> R

Unwrap and return the inner reader object

The position of the reader is undefined.

impl<R> Freeze for ZipArchive<R>

impl<R> RefUnwindSafe for ZipArchive<R>

impl<R> Send for ZipArchive<R>

impl<R> Sync for ZipArchive<R>

impl<R> Unpin for ZipArchive<R>

impl<R> UnsafeUnpin for ZipArchive<R>

impl<R> UnwindSafe for ZipArchive<R>

impl<R: $crate::clone::Clone> Clone for ZipArchive<R>

fn clone(self: &Self) -> ZipArchive<R>

impl<R: $crate::fmt::Debug> Debug for ZipArchive<R>

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

impl<T> Any for ZipArchive<R>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for ZipArchive<R>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for ZipArchive<R>

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

impl<T> CloneToUninit for ZipArchive<R>

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

impl<T> From for ZipArchive<R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Same for ZipArchive<R>

impl<T> ToOwned for ZipArchive<R>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for ZipArchive<R>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for ZipArchive<R>

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

impl<T, U> TryInto for ZipArchive<R>

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