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>
impl<W: AsyncWrite + Unpin> ZipWriter<W>
Sourcepub fn new(inner: W) -> Self
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.
Sourcepub fn with_level(self, level: Compression) -> Self
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());Sourcepub async fn append_file<'a>(
&'a mut self,
name: &str,
) -> Result<EntryWriter<'a, W>, ZipError>
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();Sourcepub async fn append_directory<'a>(
&'a mut self,
name: &str,
) -> Result<DirectoryWriter<'a, W>, ZipError>
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();Sourcepub async fn append_symlink(
&mut self,
name: &str,
target: &str,
) -> Result<(), ZipError>
pub async fn append_symlink( &mut self, name: &str, target: &str, ) -> Result<(), ZipError>
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();Sourcepub async fn finalize(self) -> Result<(), ZipError>
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.