Skip to main content

ZipWriter

Struct ZipWriter 

Source
pub struct ZipWriter<W: AsyncWrite + Unpin> { /* private fields */ }
Expand description

A streaming ZIP archive writer with per-file deflate compression.

Entries are written sequentially — each file produces its own deflate frame with a data descriptor (CRC-32 and sizes) after each entry. The output is a standard ZIP archive compatible with common unzip tools, including Windows Explorer.

§Example

use async_deflate_zip::ZipWriter;
use tokio::io::AsyncWriteExt;

let mut buf = Vec::new();
let mut zip = ZipWriter::new(&mut buf);

let mut entry = zip.append_file("hello.txt").await.unwrap();
entry.write_all(b"Hello, World!").await.unwrap();
entry.close().await.unwrap();

zip.finalize().await.unwrap();

Implementations§

Source§

impl<W: AsyncWrite + Unpin> ZipWriter<W>

Source

pub fn new(inner: W) -> Self

Create a new ZipWriter wrapping an async writer.

Uses the default compression level (Compression::default, level 6). Use with_level to customize.

Source

pub fn with_level(self, level: Compression) -> Self

Set the compression level for entries added to this archive.

Must be called before adding any entries. Returns self for chaining.

§Example
use async_deflate_zip::{ZipWriter, Compression};

let mut buf = Vec::new();
let zip = ZipWriter::new(&mut buf)
    .with_level(Compression::best());
Source

pub async fn append_file<'a>( &'a mut self, name: &str, ) -> Result<EntryWriter<'a, W>, ZipError>

Start a new file entry and return an EntryWriter for streaming data.

Writes the Local File Header, then returns an EntryWriter that compresses and buffers written data. Call EntryWriter::close to finalize the entry and write the trailing CRC-32 and sizes.

§Errors

Returns ZipError if writer is poisoned, or if writing the Local File Header fails (I/O error or field too long).

§Example
use async_deflate_zip::ZipWriter;
use tokio::io::AsyncWriteExt;

let mut buf = Vec::new();
let mut zip = ZipWriter::new(&mut buf);
let mut entry = zip.append_file("readme.txt").await.unwrap();
entry.write_all(b"content").await.unwrap();
entry.close().await.unwrap();
zip.finalize().await.unwrap();
Source

pub async fn append_directory<'a>( &'a mut self, name: &str, ) -> Result<DirectoryWriter<'a, W>, ZipError>

Start a new directory entry.

Writes the Local File Header and returns a DirectoryWriter handle. Call close to finalize the entry. Directory names should end with '/'.

§Errors

Returns ZipError if writer is poisoned, or if writing the Local File Header fails (I/O error or field too long).

§Example
use async_deflate_zip::ZipWriter;

let mut buf = Vec::new();
let mut zip = ZipWriter::new(&mut buf);
let mut dir = zip.append_directory("mydir/").await.unwrap();
dir.close().await.unwrap();
zip.finalize().await.unwrap();

Add a symbolic link entry.

The name is the path of the symlink, and target is the path the symlink points to. The target is stored uncompressed as the entry’s data content. The Central Directory entry uses S_IFLNK with VERSION_UNIX so Unix unzip tools correctly restore the symlink.

§Errors

Returns ZipError if writer is poisoned, or if writing the Local File Header, symlink target, or Data Descriptor fails (I/O error or field too long).

§Example
use async_deflate_zip::ZipWriter;

let mut buf = Vec::new();
let mut zip = ZipWriter::new(&mut buf);
zip.append_symlink("link.txt", "target.txt").await.unwrap();
zip.finalize().await.unwrap();
Source

pub async fn finalize(self) -> Result<(), ZipError>

Finalize the archive by writing the Central Directory and EOCDR.

This writes the Central Directory entries for all file and directory entries, followed by the End of Central Directory Record (and ZIP64 records if needed). The inner writer is flushed and shut down.

After calling finalize, the ZipWriter is consumed and cannot be used to add more entries.

§Errors

Returns ZipError if an entry writer is still active or the writer is poisoned, if writing the Central Directory or EOCDR fails (I/O error or field too long), or if the inner writer’s shutdown fails.

Auto Trait Implementations§

§

impl<W> Freeze for ZipWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for ZipWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for ZipWriter<W>
where W: Send,

§

impl<W> Sync for ZipWriter<W>
where W: Sync,

§

impl<W> Unpin for ZipWriter<W>

§

impl<W> UnsafeUnpin for ZipWriter<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for ZipWriter<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.