pub struct Reader<R: Read> { /* private fields */ }
Expand description
Reads one entry header/data from an archive.
Implementations§
source§impl<R: Read> Reader<R>
impl<R: Read> Reader<R>
sourcepub fn new(inner: R) -> Result<Reader<R>>
pub fn new(inner: R) -> Result<Reader<R>>
Parses metadata for the next entry in an archive, and returns a reader that will yield the entry data.
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let path = std::env::args().nth(1).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
file = reader.finish().unwrap();
}
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let path = std::env::args().nth(1).unwrap();
let filename = std::env::args().nth(2).unwrap();
let output = std::env::args().nth(3).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
if filename == reader.entry().name() {
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
let out = std::fs::File::create(&output).unwrap();
file = reader.to_writer(out).unwrap();
} else {
file = reader.skip().unwrap();
}
}
}
sourcepub fn entry(&self) -> &Entry
pub fn entry(&self) -> &Entry
Returns the metadata for this entry.
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let path = std::env::args().nth(1).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
file = reader.finish().unwrap();
}
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let path = std::env::args().nth(1).unwrap();
let filename = std::env::args().nth(2).unwrap();
let output = std::env::args().nth(3).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
if filename == reader.entry().name() {
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
let out = std::fs::File::create(&output).unwrap();
file = reader.to_writer(out).unwrap();
} else {
file = reader.skip().unwrap();
}
}
}
sourcepub fn finish(self) -> Result<R>
pub fn finish(self) -> Result<R>
Finishes reading this entry and returns the underlying reader in a position ready to read the next entry (if any).
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let path = std::env::args().nth(1).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
file = reader.finish().unwrap();
}
}
sourcepub fn to_writer<W: Write>(self, writer: W) -> Result<R>
pub fn to_writer<W: Write>(self, writer: W) -> Result<R>
Write the contents of the entry out to the writer using io::copy
, taking advantage of any
platform-specific behavior to effeciently copy data that io::copy
can use. If any of the
file data has already been read through the Read
interface, this will copy the
remaining data in the entry.
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let path = std::env::args().nth(1).unwrap();
let filename = std::env::args().nth(2).unwrap();
let output = std::env::args().nth(3).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
if filename == reader.entry().name() {
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
let out = std::fs::File::create(&output).unwrap();
file = reader.to_writer(out).unwrap();
} else {
file = reader.skip().unwrap();
}
}
}
source§impl<R: Read + Seek> Reader<R>
impl<R: Read + Seek> Reader<R>
sourcepub fn offset(&mut self) -> Result<u64>
pub fn offset(&mut self) -> Result<u64>
Returns the offset within inner, which can be useful for efficient io::copy()/copy_file_range() of file data.
sourcepub fn skip(self) -> Result<R>
pub fn skip(self) -> Result<R>
Skip past all remaining file data in this entry, returning the underlying reader in a position ready to read the next entry (if any).
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let path = std::env::args().nth(1).unwrap();
let filename = std::env::args().nth(2).unwrap();
let output = std::env::args().nth(3).unwrap();
let mut file = std::fs::File::open(path).unwrap();
loop {
let reader = cpio::NewcReader::new(file).unwrap();
if reader.entry().is_trailer() {
break;
}
if filename == reader.entry().name() {
println!(
"{} ({} bytes)",
reader.entry().name(),
reader.entry().file_size()
);
let out = std::fs::File::create(&output).unwrap();
file = reader.to_writer(out).unwrap();
} else {
file = reader.skip().unwrap();
}
}
}
Trait Implementations§
source§impl<R: Read> Read for Reader<R>
impl<R: Read> Read for Reader<R>
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 more