Crate tario

Crate tario 

Source
Expand description

A library to asynchronously read and write TAR archives.

§Writing

To write an archive, use Archive::add_entry to write the header and get back an Entry handle for writing the entry’s data.

use tokio::io::{AsyncWrite, AsyncWriteExt};
use tario::{Archive, Header};

let mut io: Vec<u8> = Vec::new();
let mut archive = Archive::new(&mut io);
let mut files = [("hello.txt", "hello world!")];

for (path, contents) in files {
  let mut header = Header::new_ustar();
  header.set_path(path)?;
  header.set_size(contents.len() as u64);
  header.set_cksum();

  let mut entry = archive.add_entry(header).await?;
  entry.write(contents.as_bytes()).await?;
}

archive.finish().await?;

§Reading

When reading an archive, use Archive::next_entry to get an Entry handle for the next file entry in the archive and read the entry’s data.

use std::io;
use tokio::io::{AsyncRead, AsyncReadExt};
use tario::Archive;

let io = io::Cursor::new(&[]); // Get a reader from somewhere
let mut archive = Archive::new(io);
let buf = &mut [0u8; 100];

while let Some(mut entry) = archive.next_entry().await? {
  loop {
    let bytes_written = entry.read(buf).await?;
    if bytes_written == 0 {
      // Reached entry EOF
      break;
    }
    // do_something_with_buffer(&buf[..bytes_written]);
  }
}

A TAR byte stream is a series of entries in order, so working with multiple entries concurrently is not possible – their data cannot be interleaved. The compiler will helpfully prevent you from doing that, so the following does not compile:

use std::io;
use tario::Archive;

let io = io::Cursor::new(&[]);
let mut archive = Archive::new(io);
let entry1 = archive.next_entry();
let entry2 = archive.next_entry();
entry1;
// error[E0499]: cannot borrow `archive` as mutable more than once at a time

Re-exports§

pub use tar as sync;

Structs§

Archive
A type that wraps an async I/O object and provides methods to read or write TAR archives.
Entry
A handle to a file entry in a TAR archive, that provides methods to read or write its data.
Header
Representation of the header of an entry in an archive

Enums§

ReadError
WriteError

Constants§

BLOCK_SIZE
A TAR byte stream is a series of 512-byte blocks.