Zipit

crates.io Documentation

🗄️ Create and stream a Zip archive into an AsyncWrite 🗄️

zipit = "0.1"

Features

Limitations

Examples

File system

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};

[tokio::main]

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(); } ```

Hyper

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) -> Result, hyper::http::Error> { let (filename1, mut fd1) = (String::from("file1.txt"), Cursor::new(b"hello\n".tovec())); let (filename2, mut fd2) = (String::from("file2.txt"), Cursor::new(b"world\n".tovec())); let archivesize = archivesize([ (filename1.asref(), fd1.getref().len()), (filename2.asref(), fd2.getref().len()), ]);

let (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)))

} ```