Expand description
§anyreader-walker
A library for reading streams of compressed and uncompressed data without knowing the format in advance.
§Example:
use anyreader_walker::{AnyWalker, FileEntry, EntryDetails, FormatKind};
use std::io::Read;
#[derive(Default)]
struct Visitor {
files: usize
}
impl AnyWalker for Visitor {
fn visit_file_entry(&mut self, entry: &mut FileEntry<impl Read>) -> std::io::Result<()> {
eprintln!("Found file: {}", entry.path().display());
self.files += 1;
Ok(())
}
fn begin_visit_archive(
&mut self,
details: &EntryDetails,
format: FormatKind,
) -> std::io::Result<bool> {
use anyreader_walker::EntryDetails;
eprintln!("Found archive: {}", details.path.display());
Ok(true)
}
}
fn main() {
let data = make_tar_zst_archive("hello world");
let mut entry = FileEntry::from_reader("input.tar.zst", data.len() as u64, data.as_slice()).unwrap();
// Or a file:
// let mut entry = FileEntry::from_path("file.tar.zst").unwrap();
let mut visitor = Visitor::default();
visitor.walk(entry).unwrap();
assert_eq!(visitor.files, 1);
}
fn make_tar_zst_archive(data: &str) -> Vec<u8> {
let mut builder = tar::Builder::new(Vec::new());
let mut header = tar::Header::new_gnu();
header.set_size(data.len() as u64);
builder.append_data(&mut header, "file-name", data.as_bytes()).unwrap();
let tar_file = builder.into_inner().unwrap();
zstd::encode_all(&tar_file[..], 1).unwrap()
}
Structs§
- A reader that contains a detected file format.
- A utility struct to keep track of the current archive stack. This is useful when processing nested archives - it supports pushing and popping archives from the stack, and provides the current nested path - including all previous nested paths.
- Represents the details of a FileEntry, including its path and size.
- A FileEntry represents a file in an archive, along with its format and size. It can be used to read the file’s contents, and can also be used to visit the contents of an archive.
Enums§
- Supported file/compression formats.