pub struct Archive<R: Read> { /* private fields */ }Expand description
A structure for reading archives.
Implementations§
Source§impl<R: Read> Archive<R>
impl<R: Read> Archive<R>
Sourcepub fn new(reader: R) -> Archive<R>
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?
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
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}Sourcepub fn variant(&self) -> Variant
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.
Sourcepub fn into_inner(self) -> Result<R>
pub fn into_inner(self) -> Result<R>
Unwrap this archive reader, returning the underlying reader object.
Sourcepub fn next_entry(&mut self) -> Option<Result<Entry<'_, R>>>
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?
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>
impl<R: Read + Seek> Archive<R>
Sourcepub fn count_entries(&mut self) -> Result<usize>
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()).
Sourcepub fn jump_to_entry(&mut self, index: usize) -> Result<Entry<'_, R>>
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().
Sourcepub fn symbols(&mut self) -> Result<Symbols<'_, R>>
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?
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}