pub struct ZipArchive<R> { /* private fields */ }
Expand description
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§
Source§impl<R> ZipArchive<R>
impl<R> ZipArchive<R>
Sourcepub fn decompressed_size(&self) -> Option<u128>
pub fn decompressed_size(&self) -> Option<u128>
Total size of the files in the archive, if it can be known. Doesn’t include directories or metadata.
Source§impl<R: Read + Seek> ZipArchive<R>
impl<R: Read + Seek> ZipArchive<R>
Sourcepub fn new(reader: R) -> ZipResult<ZipArchive<R>>
pub 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.
Sourcepub fn with_config(config: Config, reader: R) -> ZipResult<ZipArchive<R>>
pub 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.
Sourcepub fn extract<P: AsRef<Path>>(&mut self, directory: P) -> ZipResult<()>
pub fn extract<P: AsRef<Path>>(&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.
Sourcepub fn extract_unwrapped_root_dir<P: AsRef<Path>>(
&mut self,
directory: P,
root_dir_filter: impl RootDirFilter,
) -> ZipResult<()>
pub fn extract_unwrapped_root_dir<P: AsRef<Path>>( &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.”
Sourcepub fn central_directory_start(&self) -> u64
pub fn central_directory_start(&self) -> u64
Get the starting offset of the zip central directory.
Sourcepub fn offset(&self) -> u64
pub fn offset(&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.
Sourcepub fn zip64_comment(&self) -> Option<&[u8]>
pub fn zip64_comment(&self) -> Option<&[u8]>
Get the ZIP64 comment of the zip archive, if it is ZIP64.
Sourcepub fn file_names(&self) -> impl Iterator<Item = &str>
pub fn file_names(&self) -> impl Iterator<Item = &str>
Returns an iterator over all the file and directory names in this archive.
Sourcepub fn by_name_decrypt(
&mut self,
name: &str,
password: &[u8],
) -> ZipResult<ZipFile<'_>>
pub fn by_name_decrypt( &mut self, name: &str, password: &[u8], ) -> ZipResult<ZipFile<'_>>
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.
Sourcepub fn by_name(&mut self, name: &str) -> ZipResult<ZipFile<'_>>
pub fn by_name(&mut self, name: &str) -> ZipResult<ZipFile<'_>>
Search for a file entry by name
Sourcepub fn index_for_name(&self, name: &str) -> Option<usize>
pub fn index_for_name(&self, name: &str) -> Option<usize>
Get the index of a file entry by name, if it’s present.
Sourcepub fn index_for_path<T: AsRef<Path>>(&self, path: T) -> Option<usize>
pub fn index_for_path<T: AsRef<Path>>(&self, path: T) -> Option<usize>
Get the index of a file entry by path, if it’s present.
Sourcepub fn name_for_index(&self, index: usize) -> Option<&str>
pub fn name_for_index(&self, index: usize) -> Option<&str>
Get the name of a file entry, if it’s present.
Sourcepub fn by_name_seek(&mut self, name: &str) -> ZipResult<ZipFileSeek<'_, R>>
pub fn by_name_seek(&mut self, name: &str) -> ZipResult<ZipFileSeek<'_, R>>
Search for a file entry by name and return a seekable object.
Sourcepub fn by_index_seek(&mut self, index: usize) -> ZipResult<ZipFileSeek<'_, R>>
pub fn by_index_seek(&mut self, index: usize) -> ZipResult<ZipFileSeek<'_, R>>
Search for a file entry by index and return a seekable object.
Sourcepub fn by_index_decrypt(
&mut self,
file_number: usize,
password: &[u8],
) -> ZipResult<ZipFile<'_>>
pub fn by_index_decrypt( &mut self, file_number: usize, password: &[u8], ) -> ZipResult<ZipFile<'_>>
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.
Sourcepub fn by_index(&mut self, file_number: usize) -> ZipResult<ZipFile<'_>>
pub fn by_index(&mut self, file_number: usize) -> ZipResult<ZipFile<'_>>
Get a contained file by index
Sourcepub fn by_index_raw(&mut self, file_number: usize) -> ZipResult<ZipFile<'_>>
pub fn by_index_raw(&mut self, file_number: usize) -> ZipResult<ZipFile<'_>>
Get a contained file by index without decompressing it
Sourcepub fn root_dir(&self, filter: impl RootDirFilter) -> ZipResult<Option<PathBuf>>
pub fn root_dir(&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
.
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Unwrap and return the inner reader object
The position of the reader is undefined.
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