Hi guys, I want to take a stream, turn it into a zip file and stream that out but I'm having problems, specifically, I don't understand why I get this panic, in this minimal example: (see the comment)
use async_zip::{
write::{EntryOptions, ZipFileWriter},
Compression,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() {
let dummy_file: &[u8] = b"hello world";
let (mut compressed_rx, mut compressed_tx) = tokio::io::duplex(1024);
tokio::spawn(async move {
let mut writer = ZipFileWriter::new(&mut compressed_tx);
let mut entry_writer = writer
.write_entry_stream(EntryOptions::new(
"simple.txt".to_string(),
Compression::Stored,
))
.await
.unwrap();
entry_writer.write_all(dummy_file).await.unwrap();
entry_writer.close().await.unwrap(); // panicked at 'called `Result::unwrap()` on an `Err` value: UpstreamReadError(Kind(BrokenPipe))
writer.close().await.unwrap();
});
// "compressed_rx" should have our stream, if successful...
// but for testing here, lets collect it all into memory and write to file
let mut output = Vec::new();
compressed_rx.read_to_end(&mut output).await.unwrap();
tokio::fs::write("tmp/arif.zip", &output).await.unwrap();
}
Any help is greatly appreciated!!
Note, that creating:
let mut file = tokio::fs::File::create("/tmp/foo.zip").await.unwrap();
and passing &mut file
to ZipFileWriter::new()
with an .await
on the end of the tokio::spawn() does produce expected results
I guess I could probably solve this by streamingly write to a File, and at the same time streamingly read from that File (I hope/wonder if it's safe if the client reads from the file faster than the ZipFileWriter writes to it?)