Crate fuchsia_archive

source ·
Expand description

§Reading, Writing and Listing Fuchsia Archives (FAR) Data

This crate is a Rust port of the Go Far package.

§Example

use anyhow::Error;
use std::collections::BTreeMap;
use std::fs;
use std::io::{Cursor, Read, Write};
use tempfile::TempDir;

fn create_test_files(file_names: &[&str]) -> Result<TempDir, Error> {
    let tmp_dir = TempDir::new()?;
    for file_name in file_names {
        let file_path = tmp_dir.path().join(file_name);
        let parent_dir = file_path.parent().unwrap();
        fs::create_dir_all(&parent_dir)?;
        let file_path = tmp_dir.path().join(file_name);
        let mut tmp_file = fs::File::create(&file_path)?;
        writeln!(tmp_file, "{}", file_name)?;
    }
    Ok(tmp_dir)
}

let file_names = ["b", "a", "dir/c"];
let test_dir = create_test_files(&file_names).unwrap();
let mut path_content_map: BTreeMap<&str, (u64, Box<dyn Read>)> = BTreeMap::new();
for file_name in file_names.iter() {
    let file = fs::File::open(test_dir.path().join(file_name)).unwrap();
    path_content_map.insert(file_name, (file.metadata().unwrap().len(), Box::new(file)));
}
let mut result = Vec::new();
fuchsia_archive::write(&mut result, path_content_map).unwrap();
let result = &result[..];

let reader = fuchsia_archive::Reader::new(Cursor::new(result)).unwrap();
let entries = reader.list().map(|e| e.path()).collect::<Vec<_>>();
assert_eq!(entries, ["a", "b", "dir/c"]);

Structs§

  • A struct to open and read a FAR-formatted archive asynchronously.
  • A struct to open and read a FAR-formatted archive asynchronously. Requires that all paths are valid UTF-8.
  • An entry in an archive, returned by Reader::list
  • A struct to open and read FAR-formatted archive.
  • An entry in a UTF-8 archive, returned by Reader::list
  • A struct to open and read FAR-formatted archive. Requires that all paths are valid UTF-8.

Enums§

Constants§

Functions§

  • Write a FAR-formatted archive to the target.

Type Aliases§