How to best stream a zip archive to stdout?

I am creating a zip archive dynamically using the superb zip crate and then streaming that archive to stdout.

The relevant snippet is:

It seemed odd to me to have to persist something to disk when the program generates the data and archive in memory.

Are there better ways to accomplish this?

Or what I am missing about the traits that ZipWriter implements that could help here?

Rust has a io::Write trait that is implemented by lots of things, including Vec, File, and stdout. They all work the same.

So don't open a File, don't give file to the library. Give it stdout, and it'll write to stdout.

1 Like

Looks like you'll need W: Write + Seek for ZipWriter, so stdout won't work (no Seek) but there are in-memory writers you can use. For example Cursor::new(Vec::new()) should work (using std::io::Cursor).

You might see Seek and wonder how to solve it, but you can always hit that doc page and see what implementors there are.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.