Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of an alternative Builder which impl io::Read #316

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

ph0llux
Copy link

@ph0llux ph0llux commented Apr 12, 2023

Hi,

I have implemented an alternative Builder (I've named it "Streamer", but I'm not very good at such naming, so feel free to suggest something more appropriate 😄 ) which implements io::Read.

How does it work?

You build a streamer, then add your stuff (e.g. some paths to files) and then "read" the archive. You don't need an underlying writer, you can use it directly as a reader.
Here is a code example:

use std::path::PathBuf;
use std::fs;
use tar::Streamer;
use std::io;

fn main() {
    let mut streamer = Streamer::new();
    // Use the directory at one location, but insert it into the archive
    // with a different name.
    streamer.append_dir_all("my_download_dir", "/home/ph0llux/Downloads").unwrap();
    // Write the archive to the given path.
    let mut target_archive = fs::File::create("/home/ph0llux/my_downloads.tar").unwrap();
    io::copy(&mut streamer, &mut target_archive).unwrap();
}

The biggest question: Why?

Yes... my personal use case was the use of the Rocket-library - I had to build a TAR archive from some files which are on a web server and then offer that directly for download.
Since the files are very large, I always had to "write" the tar archive to the web server first and then I could offer it for download.
With the streamer I can offer it directly as a data stream and let it "build on-the-fly" during the download.
But there may be other use-cases, also.

When will I get an I/O error?

Mostly while reading the archive.

Additional Notes

  • I tried to recreate the "public API" of the [Builder] as far as possible in the [Streamer]. I.e. if [Builder] provides an append_data(), [Streamer] does the same.
  • No code review of a third person has occurred to yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant