12 releases (breaking)

Uses old Rust 2015

0.9.0 Aug 16, 2021
0.8.0 Aug 31, 2019
0.7.0 May 2, 2019
0.6.2 Dec 2, 2018
0.2.0 Mar 4, 2017

#652 in Parser implementations

Download history 18803/week @ 2024-07-21 19587/week @ 2024-07-28 18690/week @ 2024-08-04 21174/week @ 2024-08-11 19296/week @ 2024-08-18 19895/week @ 2024-08-25 23719/week @ 2024-09-01 21718/week @ 2024-09-08 22078/week @ 2024-09-15 22908/week @ 2024-09-22 23942/week @ 2024-09-29 24468/week @ 2024-10-06 28741/week @ 2024-10-13 25863/week @ 2024-10-20 26280/week @ 2024-10-27 30185/week @ 2024-11-03

113,288 downloads per month
Used in 76 crates (42 directly)

MIT license

80KB
1.5K SLoC

rust-ar

Build Status Build status

A rust library for encoding/decoding Unix archive (.a) files.

Documentation: https://docs.rs/ar

Overview

The ar crate is a pure Rust implementation of a Unix archive file reader and writer. This library provides a streaming interface, similar to that of the tar crate, that avoids having to ever load a full archive entry into memory.

License

rust-ar is made available under the MIT License.


lib.rs:

A library for encoding/decoding Unix archive files.

This library provides utilities necessary to manage Unix archive files (as generated by the standard ar command line utility) abstracted over a reader or writer. This library provides a streaming interface that avoids having to ever load a full archive entry into memory.

The API of this crate is meant to be similar to that of the tar crate.

Format variants

Unix archive files come in several variants, of which three are the most common:

  • The common variant, used for Debian package (.deb) files among other things, which only supports filenames up to 16 characters.
  • The BSD variant, used by the ar utility on BSD systems (including Mac OS X), which is backwards-compatible with the common variant, but extends it to support longer filenames and filenames containing spaces.
  • The GNU variant, used by the ar utility on GNU and many other systems (including Windows), which is similar to the common format, but which stores filenames in a slightly different, incompatible way, and has its own strategy for supporting long filenames.

This crate supports reading and writing all three of these variants.

Example usage

Writing an archive:

use ar::Builder;
use std::fs::File;
// Create a new archive that will be written to foo.a:
let mut builder = Builder::new(File::create("foo.a").unwrap());
// Add foo/bar.txt to the archive, under the name "bar.txt":
builder.append_path("foo/bar.txt").unwrap();
// Add foo/baz.txt to the archive, under the name "hello.txt":
let mut file = File::open("foo/baz.txt").unwrap();
builder.append_file(b"hello.txt", &mut file).unwrap();

Reading an archive:

use ar::Archive;
use std::fs::File;
use std::io;
use std::str;
// Read an archive from the file foo.a:
let mut archive = Archive::new(File::open("foo.a").unwrap());
// Iterate over all entries in the archive:
while let Some(entry_result) = archive.next_entry() {
    let mut entry = entry_result.unwrap();
    // Create a new file with the same name as the archive entry:
    let mut file = File::create(
        str::from_utf8(entry.header().identifier()).unwrap(),
    ).unwrap();
    // The Entry object also acts as an io::Read, so we can easily copy the
    // contents of the archive entry into the file:
    io::copy(&mut entry, &mut file).unwrap();
}

No runtime deps