Archive

Struct Archive 

Source
pub struct Archive<R: Read> { /* private fields */ }
Expand description

A structure for reading archives.

Implementations§

Source§

impl<R: Read> Archive<R>

Source

pub fn new(reader: R) -> Archive<R>

Create a new archive reader with the underlying reader object as the source of all data read.

Examples found in repository?
examples/symbols.rs (line 26)
15fn main() {
16    let num_args = env::args().count();
17    if num_args != 2 {
18        println!("Usage: symbols <path/to/archive.a>");
19        return;
20    }
21
22    let input_path = env::args().nth(1).unwrap();
23    let input_path = Path::new(&input_path);
24    let input_file =
25        File::open(input_path).expect("failed to open input file");
26    let mut archive = ar::Archive::new(input_file);
27
28    for symbol in archive.symbols().expect("failed to parse symbols") {
29        println!("{}", String::from_utf8_lossy(symbol));
30    }
31}
More examples
Hide additional examples
examples/extract.rs (line 34)
23fn main() {
24    let num_args = env::args().count();
25    if num_args != 2 {
26        println!("Usage: extract <path/to/archive.a>");
27        return;
28    }
29
30    let input_path = env::args().nth(1).unwrap();
31    let input_path = Path::new(&input_path);
32    let input_file =
33        File::open(input_path).expect("failed to open input file");
34    let mut archive = ar::Archive::new(input_file);
35
36    while let Some(entry) = archive.next_entry() {
37        let mut entry = entry.expect("failed to parse archive entry");
38        let output_path = Path::new(
39            str::from_utf8(entry.header().identifier())
40                .expect("Non UTF-8 filename"),
41        )
42        .to_path_buf();
43        let mut output_file = File::create(&output_path)
44            .expect(&format!("unable to create file {:?}", output_path));
45        io::copy(&mut entry, &mut output_file)
46            .expect(&format!("failed to extract file {:?}", output_path));
47    }
48}
Source

pub fn variant(&self) -> Variant

Returns which format variant this archive appears to be so far.

Note that this may not be accurate before the archive has been fully read (i.e. before the next_entry() method returns None). In particular, a new Archive object that hasn’t yet read any data at all will always return Variant::Common.

Source

pub fn into_inner(self) -> Result<R>

Unwrap this archive reader, returning the underlying reader object.

Source

pub fn next_entry(&mut self) -> Option<Result<Entry<'_, R>>>

Reads the next entry from the archive, or returns None if there are no more.

Examples found in repository?
examples/extract.rs (line 36)
23fn main() {
24    let num_args = env::args().count();
25    if num_args != 2 {
26        println!("Usage: extract <path/to/archive.a>");
27        return;
28    }
29
30    let input_path = env::args().nth(1).unwrap();
31    let input_path = Path::new(&input_path);
32    let input_file =
33        File::open(input_path).expect("failed to open input file");
34    let mut archive = ar::Archive::new(input_file);
35
36    while let Some(entry) = archive.next_entry() {
37        let mut entry = entry.expect("failed to parse archive entry");
38        let output_path = Path::new(
39            str::from_utf8(entry.header().identifier())
40                .expect("Non UTF-8 filename"),
41        )
42        .to_path_buf();
43        let mut output_file = File::create(&output_path)
44            .expect(&format!("unable to create file {:?}", output_path));
45        io::copy(&mut entry, &mut output_file)
46            .expect(&format!("failed to extract file {:?}", output_path));
47    }
48}
Source§

impl<R: Read + Seek> Archive<R>

Source

pub fn count_entries(&mut self) -> Result<usize>

Scans the archive and returns the total number of entries in the archive (not counting special entries, such as the GNU archive name table or symbol table, that are not returned by next_entry()).

Source

pub fn jump_to_entry(&mut self, index: usize) -> Result<Entry<'_, R>>

Scans the archive and jumps to the entry at the given index. Returns an error if the index is not less than the result of count_entries().

Source

pub fn symbols(&mut self) -> Result<Symbols<'_, R>>

Scans the archive and returns an iterator over the symbols in the archive’s symbol table. If the archive doesn’t have a symbol table, this method will still succeed, but the iterator won’t produce any values.

Examples found in repository?
examples/symbols.rs (line 28)
15fn main() {
16    let num_args = env::args().count();
17    if num_args != 2 {
18        println!("Usage: symbols <path/to/archive.a>");
19        return;
20    }
21
22    let input_path = env::args().nth(1).unwrap();
23    let input_path = Path::new(&input_path);
24    let input_file =
25        File::open(input_path).expect("failed to open input file");
26    let mut archive = ar::Archive::new(input_file);
27
28    for symbol in archive.symbols().expect("failed to parse symbols") {
29        println!("{}", String::from_utf8_lossy(symbol));
30    }
31}

Auto Trait Implementations§

§

impl<R> Freeze for Archive<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Archive<R>
where R: RefUnwindSafe,

§

impl<R> Send for Archive<R>
where R: Send,

§

impl<R> Sync for Archive<R>
where R: Sync,

§

impl<R> Unpin for Archive<R>
where R: Unpin,

§

impl<R> UnwindSafe for Archive<R>
where R: UnwindSafe,

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, 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.