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>
impl<'a> ArchiveReader<'a>
Sourcepub fn tar<R: Read + Send + 'a>(
reader: R,
codec: Option<CompressionCodec>,
) -> Self
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.
Sourcepub fn seekable<R: Read + Seek + Send + 'a>(
reader: R,
format: SeekableFormat,
) -> Self
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.
Sourcepub fn of_type<R: Read + Seek + Send + 'a>(
reader: R,
format: ArchiveType,
) -> Self
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);Sourcepub fn from_bytes<B: AsRef<[u8]> + Send + 'a>(
bytes: B,
format: Option<ArchiveType>,
) -> Result<Self>
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);Sourcepub fn detect<R: Read + Seek + Send + 'a>(reader: R) -> Result<Self>
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);Sourcepub fn detect_stream<R: Read + Send + 'a>(reader: R) -> Result<Self>
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
)))
);Sourcepub const fn format(&self) -> ArchiveType
pub const fn format(&self) -> ArchiveType
The format the reader was declared to hold.