pub struct File<'a> { /* private fields */ }
Expand description
A file in the archive.
File
objects allow standard operations on file inodes in an archive. File
implements
Read
and Seek
, so anything that reads files using standard Rust semantics can interact
natively with these files. to_bytes
and to_string
offer convenience wrappers around this. Files that were archived with compression and
fragmentation disabled can also be mmap
ed and accessed as an ordinary byte
array.
let archive = Archive::new("archive.sfs")?;
let node = archive.get("/a/01.txt")?.unwrap().resolve()?;
let file = node.as_file()?;
// File can now be used like anything else that implements `Read` and `Seek`.
let mut buf = [0; 10];
file.seek(SeekFrom::End(-10))?;
file.read(&mut buf)?;
Implementations§
Source§impl<'a> File<'a>
impl<'a> File<'a>
Sourcepub fn to_bytes(&mut self) -> Result<Vec<u8>>
pub fn to_bytes(&mut self) -> Result<Vec<u8>>
Retrieve the entire contents of the file in the form of a byte Vec.
Sourcepub fn to_string(&mut self) -> Result<String>
pub fn to_string(&mut self) -> Result<String>
Retrieve the entire contents of the file in the form of a String.
This calls Read::read_to_string
under the hood. Consequently, a UTF-8 error
will be raised if the entire file is not valid UTF-8.
Sourcepub fn mmap<'b>(&'b mut self) -> Option<&'b [u8]>
pub fn mmap<'b>(&'b mut self) -> Option<&'b [u8]>
Map a file into memory for fast parallel random access.
This uses mmap
to map the file into memory. It will fail and return None
if the
file is compressed or fragmented. If the DontCompress
and DontFragment
options are set for a file at
archive creation time, it will be added to the archive in one contiguous unmodified chunk.
This is necessary because mmap
provides a view into a file exactly as it is on-disk;
there is no opportunity for the library to apply decompression or other transformations
when mapping.
let map = file.mmap().expect("File is not mmappable");
println!("{}", str::from_utf8(map)?);
Trait Implementations§
Source§impl<'a> Read for File<'a>
impl<'a> Read for File<'a>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<'a> Seek for File<'a>
impl<'a> Seek for File<'a>
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)