#archive #format #tar-archive #data #reader #read

anyreader

A library for reading compressed and uncompressed data without knowing the format in advance

4 releases

0.2.0 Dec 31, 2024
0.1.2 Dec 30, 2024
0.1.1 Dec 29, 2024
0.1.0 Dec 28, 2024

#241 in Compression

Download history 325/week @ 2024-12-24 200/week @ 2024-12-31

525 downloads per month
Used in 2 crates (via anyreader-walker)

MIT license

24KB
437 lines

anyreader

This crate provides a simple way to detect and read compressed streams of data in a transparent way. It supports the following compression formats:

  • Gzip
  • Zstd
  • Bzip2
  • Xz

And it can detect the following archive formats:

  • Tar
  • Zip

Example: Reading compressed streams

use anyreader::AnyReader;
use std::fs::File;

fn main() {
    // Supports compressed files/data
    let data = zstd::encode_all("hello world".as_bytes(), 1).unwrap();
    // Or a file:
    // let data = File::open("file.zstd").unwrap();
    let mut reader = AnyReader::from_reader(data.as_slice()).unwrap();
    assert!(reader.is_zst());
    // Read the data
    assert_eq!(std::io::read_to_string(&mut reader).unwrap(), "hello world");
}

Example: Detecting archive types

use anyreader::{AnyFormat, FormatKind};
use std::fs::File;
use tar::Archive;

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()
}

fn main() {
    let data = make_tar_zst_archive("hello world");
    // Or a file:
    // let data = File::open("file.tar.zst").unwrap();
    let reader = AnyFormat::from_reader(data.as_slice()).unwrap();
    assert_eq!(reader.kind, FormatKind::Tar);
    let mut archive = tar::Archive::new(reader);
    // Process the archive
    for entry in archive.entries().unwrap() {
        println!("{:?}", entry.unwrap().path());
    }
}

Dependencies

~9–18MB
~252K SLoC