🗄️ Create and stream a Zip archive into an AsyncWrite 🗄️
zipit = "0.1"
Content-Length
before streaming).tokio
's AsyncRead
and AsyncWrite
traits.Write a Zip archive to the file system using tokio::fs::File
:
```rust use std::io::Cursor; use tokio::fs::File; use zipit::{Archive, FileDateTime};
async fn main() { let file = File::create("archive.zip").await.unwrap(); let mut archive = Archive::new(file); archive.append( "file1.txt".toowned(), FileDateTime::now(), &mut Cursor::new(b"hello\n".tovec()), ).await.unwrap(); archive.append( "file2.txt".toowned(), FileDateTime::now(), &mut Cursor::new(b"world\n".tovec()), ).await.unwrap(); archive.finalize().await.unwrap(); } ```
Stream a Zip archive as a hyper
reponse:
```rust use std::io::Cursor; use hyper::{header, Body, Request, Response, Server, StatusCode}; use tokio::io::duplex; use tokioutil::io::ReaderStream; use zipit::{archivesize, Archive, FileDateTime};
async fn ziparchive(req: Request
) -> Resultlet (w, r) = duplex(4096);
tokio::spawn(async move {
let mut archive = Archive::new(w);
archive
.append(
filename_1,
FileDateTime::now(),
&mut fd_1,
)
.await
.unwrap();
archive
.append(
filename_2,
FileDateTime::now(),
&mut fd_2,
)
.await
.unwrap();
archive.finalize().await.unwrap();
});
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_LENGTH, archive_size)
.header(header::CONTENT_TYPE, "application/zip")
.body(Body::wrap_stream(ReaderStream::new(r)))
} ```