Skip to main content

ArchiveReader

Struct ArchiveReader 

Source
pub struct ArchiveReader<'a> { /* private fields */ }
Expand description

An archive supplied as a reader.

§Examples

use exarch_core::ArchiveReader;
use exarch_core::SeekableFormat;
use exarch_core::formats::detect::ArchiveType;
use std::io::Cursor;

let tar = ArchiveReader::tar(Cursor::new(Vec::<u8>::new()), None);
assert_eq!(tar.format(), ArchiveType::Tar);

let zip = ArchiveReader::seekable(Cursor::new(Vec::<u8>::new()), SeekableFormat::Zip);
assert_eq!(zip.format(), ArchiveType::Zip);

Implementations§

Source§

impl<'a> ArchiveReader<'a>

Source

pub fn tar<R: Read + Send + 'a>( reader: R, codec: Option<CompressionCodec>, ) -> Self

A TAR archive in reader, compressed with codec when given.

The reader holds the raw bytes. The decoder is applied when the operation runs, with the decoder memory limit of its config.

Source

pub fn seekable<R: Read + Seek + Send + 'a>( reader: R, format: SeekableFormat, ) -> Self

A ZIP or 7z archive in reader.

The archive must start at offset 0 of the reader: 7z addresses its blocks by absolute offsets, so an archive embedded in a larger stream needs a reader that maps offset 0 to the start of the archive.

Source

pub fn of_type<R: Read + Seek + Send + 'a>( reader: R, format: ArchiveType, ) -> Self

An archive of the stated format in reader, whichever way it is read.

The reader must start at the archive, as for Self::tar and Self::seekable.

§Examples
use exarch_core::ArchiveReader;
use exarch_core::formats::detect::ArchiveType;
use std::io::Cursor;

let reader = ArchiveReader::of_type(Cursor::new(Vec::<u8>::new()), ArchiveType::TarGz);
assert_eq!(reader.format(), ArchiveType::TarGz);
Source

pub fn from_bytes<B: AsRef<[u8]> + Send + 'a>( bytes: B, format: Option<ArchiveType>, ) -> Result<Self>

An archive held in memory: of the stated format, or detected from its leading bytes when format is None.

§Errors

With format of None, the errors of Self::detect.

§Examples
use exarch_core::ArchiveReader;
use exarch_core::formats::detect::ArchiveType;

let bytes = b"PK\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".to_vec();
let detected = ArchiveReader::from_bytes(bytes.clone(), None)?;
assert_eq!(detected.format(), ArchiveType::Zip);

let stated = ArchiveReader::from_bytes(bytes, Some(ArchiveType::Tar))?;
assert_eq!(stated.format(), ArchiveType::Tar);
Source

pub fn detect<R: Read + Seek + Send + 'a>(reader: R) -> Result<Self>

Detects the format of the archive at the current position of a seekable reader from its leading bytes.

The reader is left at the position it was in. A TAR archive may start anywhere; a ZIP or 7z archive must start at offset 0, because both address their records by absolute offsets. Where several signatures match, the first in table order wins: a container magic at the start beats the ustar marker at offset 257, and so do gzip, xz and zstd magics. Only BZh can open a member name, so a checksum-valid ustar header that does not start with a complete bzip2 stream header is read as a plain TAR; input valid under both readings stays bzip2. A v7 TAR (no ustar marker) and a ZIP with data prepended are not recognized; use Self::tar for the former.

§Errors

Returns ArchiveError::ReaderFormat with Unrecognized if no signature matches, or with NotAtStart if a ZIP or 7z signature is found at a position other than 0. Returns ArchiveError::Io if the reader cannot be read or repositioned.

§Examples
use exarch_core::ArchiveReader;
use exarch_core::formats::detect::ArchiveType;
use std::io::Cursor;

let empty_zip = b"PK\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
let reader = ArchiveReader::detect(Cursor::new(empty_zip.to_vec()))?;
assert_eq!(reader.format(), ArchiveType::Zip);
Source

pub fn detect_stream<R: Read + Send + 'a>(reader: R) -> Result<Self>

Detects the format of a forward-only reader from its leading bytes.

Only TAR streams can be read without seeking. The bytes consumed for detection are replayed in front of the rest of the stream, so nothing is lost. The signature rules are those of Self::detect. Up to 512 bytes are peeked, or the stream is read to its end if it is shorter.

§Errors

Returns ArchiveError::ReaderFormat with NeedsSeek if the bytes are a ZIP or 7z signature, or with Unrecognized if no signature matches. Returns ArchiveError::Io if the reader cannot be read.

§Examples
use std::assert_matches;

use exarch_core::ArchiveError;
use exarch_core::ArchiveReader;
use exarch_core::ReaderFormatError;
use exarch_core::SeekableFormat;

let zip: &[u8] = b"PK\x03\x04 and the rest";
assert_matches!(
    ArchiveReader::detect_stream(zip),
    Err(ArchiveError::ReaderFormat(ReaderFormatError::NeedsSeek(
        SeekableFormat::Zip
    )))
);
Source

pub const fn format(&self) -> ArchiveType

The format the reader was declared to hold.

Trait Implementations§

Source§

impl Debug for ArchiveReader<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for ArchiveReader<'a>

§

impl<'a> !Sync for ArchiveReader<'a>

§

impl<'a> !UnwindSafe for ArchiveReader<'a>

§

impl<'a> Freeze for ArchiveReader<'a>

§

impl<'a> Send for ArchiveReader<'a>

§

impl<'a> Unpin for ArchiveReader<'a>

§

impl<'a> UnsafeUnpin for ArchiveReader<'a>

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<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.