Skip to main content

ZipFile

Struct ZipFile 

Source
pub struct ZipFile<'a, R: Read + ?Sized> { /* private fields */ }
Expand description

A struct for reading a zip file

When reading from a ZipFile using Self::read(), keep in mind that read() does not guarantee the buffer will be fully filled in a single call.

If your logic depends on the buffer being completely populated, use Self::read_exact() instead. It will continue reading until the entire buffer is filled or an error occurs.

Implementations§

Source§

impl<'a, R: Read + ?Sized> ZipFile<'a, R>

Methods for retrieving information on zip files

Source

pub fn name_raw(&self) -> &[u8] ⓘ

Get the name of the file, in the raw (internal) byte representation.

The encoding of this data is currently undefined.

Source

pub fn name(&self) -> ZipResult<Cow<'_, str>>

Get the name of the file

§Warnings

It is dangerous to use this name directly when extracting an archive. It may contain an absolute path (/etc/shadow), or break out of the current directory (../runtime). Carelessly writing to these paths allows an attacker to craft a ZIP archive that will overwrite critical files.

You can use the ZipFile::enclosed_name method to validate the name as a safe path.

Source

pub fn mangled_name(&self) -> ZipResult<PathBuf>

Rewrite the path, ignoring any path components with special meaning.

  • Absolute paths are made relative
  • ParentDirs are ignored
  • Truncates the filename at a NULL byte

This is appropriate if you need to be able to extract something from any archive, but will easily misrepresent trivial paths like foo/../bar as foo/bar (instead of bar). Because of this, ZipFile::enclosed_name is the better option in most scenarios.

Source

pub fn enclosed_name(&self) -> Option<PathBuf>

Ensure the file path is safe to use as a Path.

  • It can’t contain NULL bytes
  • It can’t resolve to a path outside the current directory

    foo/../bar is fine, foo/../../bar is not.

  • It can’t be an absolute path

This will read well-formed ZIP files correctly, and is resistant to path-based exploits. It is recommended over ZipFile::mangled_name.

Source

pub fn header_start(&self) -> u64

Get the starting offset of the zip header for this file

Source

pub fn central_header_start(&self) -> u64

Get the starting offset of the zip header in the central directory for this file

Source

pub fn version_made_by(&self) -> (u8, u8)

Get the version of the file

Source

pub fn version_needed(&self) -> u16

PKZIP version needed to open this file (from APPNOTE 4.4.3.2).

Source

pub fn comment(&self) -> &str

Get the comment of the file

Source

pub fn system(&self) -> System

Get the system of the file

Source

pub fn external_attributes(&self) -> u32

Get the external attributes of the file

Source

pub fn flags(&self) -> ZipFileFlags

Get the flags of the file

Source

pub fn compression(&self) -> CompressionMethod

Get the compression method used to store the file

Source

pub fn encrypted(&self) -> bool

Get if the files is encrypted or not

Source

pub fn compressed_size(&self) -> u64

Get the size of the file, in bytes, in the archive

Source

pub fn size(&self) -> u64

Get the size of the file, in bytes, when uncompressed

Source

pub fn last_modified(&self) -> Option<DateTime>

Get the time the file was last modified

Source

pub fn is_dir(&self) -> bool

Returns whether the file is actually a directory

Returns whether the file is actually a symbolic link

Source

pub fn is_file(&self) -> bool

Returns whether the file is a normal file (i.e. not a directory or symlink)

Source

pub fn is_read_only(&self) -> bool

Returns whether the file is read-only. (On Unix, this means it’s not readable by anyone.)

Source

pub fn unix_mode(&self) -> Option<u32>

Get unix mode for the file

Source

pub fn crc32(&self) -> u32

Get the CRC32 hash of the original file

Source

pub fn extra_data(&self) -> Option<Vec<u8>>

Get the extra data of the zip header for this file

Source

pub fn data_start(&self) -> Option<u64>

Get the starting offset of the data of the compressed file

Source

pub fn options(&self) -> SimpleFileOptions

Get the SimpleFileOptions that would be used to write this file to a new zip archive.

Source§

impl<R: Read> ZipFile<'_, R>

Methods for retrieving information on zip files

Source

pub fn extra_data_fields(&self) -> impl Iterator<Item = &ExtraField>

iterate through all extra fields

Trait Implementations§

Source§

impl<'a, R: Debug + Read + ?Sized> Debug for ZipFile<'a, R>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<R: Read + ?Sized> Drop for ZipFile<'_, R>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<R: Read + ?Sized> HasZipMetadata for ZipFile<'_, R>

Source§

fn get_metadata(&self) -> &ZipFileData

Get the file metadata
Source§

impl<R: Read + ?Sized> Read for ZipFile<'_, R>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>

Reads all bytes until EOF in this source, placing them into buf. Read more
Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize>

Reads all bytes until EOF in this source, appending them to buf. Read more
Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>

Reads the exact number of bytes required to fill buf. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self> ⓘ
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self> ⓘ
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

fn read_le<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in little-endian order. Read more
Source§

fn read_be<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in big-endian order. Read more

Auto Trait Implementations§

§

impl<'a, R> !Freeze for ZipFile<'a, R>

§

impl<'a, R> !UnwindSafe for ZipFile<'a, R>

§

impl<'a, R> RefUnwindSafe for ZipFile<'a, R>
where R: RefUnwindSafe + ?Sized,

§

impl<'a, R> Send for ZipFile<'a, R>
where R: Send + ?Sized,

§

impl<'a, R> Sync for ZipFile<'a, R>
where R: Sync + ?Sized,

§

impl<'a, R> Unpin for ZipFile<'a, R>
where R: ?Sized,

§

impl<'a, R> UnsafeUnpin for ZipFile<'a, R>
where R: ?Sized,

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, 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<R> LittleEndianReadExt for R
where R: Read,

Source§

fn read_u16_le(&mut self) -> Result<u16>

Read a u16 as little endian
Source§

fn read_u32_le(&mut self) -> Result<u32>

Read a u32 as little endian
Source§

fn read_u64_le(&mut self) -> Result<u64>

Read a u64 as little endian
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.