ZipArchiveWriter

Struct ZipArchiveWriter 

Source
pub struct ZipArchiveWriter<W> { /* private fields */ }
Expand description

Create a new Zip archive.

Basic usage:

use std::io::Write;

let mut output = std::io::Cursor::new(Vec::new());
let mut archive = rawzip::ZipArchiveWriter::new(&mut output);
let (mut entry, config) = archive.new_file("file.txt").start().unwrap();
let mut writer = config.wrap(&mut entry);
writer.write_all(b"Hello, world!").unwrap();
let (_, output) = writer.finish().unwrap();
entry.finish(output).unwrap();
archive.finish().unwrap();

Use the builder for customization:

use std::io::Write;

let mut output = std::io::Cursor::new(Vec::<u8>::new());
let mut _archive = rawzip::ZipArchiveWriter::builder()
    .with_capacity(1000)  // Optimize for 1000 anticipated files
    .build(&mut output);
// ... add files as usual

Implementations§

Source§

impl ZipArchiveWriter<()>

Source

pub fn builder() -> ZipArchiveWriterBuilder

Creates a ZipArchiveWriterBuilder for configuring the writer.

Source§

impl<W> ZipArchiveWriter<W>

Source

pub fn new(writer: W) -> Self

Creates a new ZipArchiveWriter that writes to writer.

Source

pub fn stream_offset(&self) -> u64

Returns the current offset in the output stream.

Analagous to std::io::Cursor::position.

This can be used to determine various offsets during ZIP archive creation:

  • Local header offset
  • Start of compressed data offset
  • End of compressed data offset
  • End of data descriptor offset / next file’s local header offset
§Example
use std::io::Write;

let mut output = std::io::Cursor::new(Vec::new());
let mut archive = rawzip::ZipArchiveWriter::new(&mut output);

// 1. Get local header offset
let local_header_offset = archive.stream_offset();
let mut file = archive.new_file("test.txt").create().unwrap();

// 2. Get start of data offset
let data_start_offset = file.stream_offset();

// Write some data
let mut writer = rawzip::ZipDataWriter::new(&mut file);
writer.write_all(b"Hello World").unwrap();
let (_, desc) = writer.finish().unwrap();

// 3. Get end of compressed data offset
let end_data_offset = file.stream_offset();

let compressed_bytes = file.finish(desc).unwrap();

// 4. Get end of data descriptor offset (next file's local header offset)
let end_descriptor_offset = archive.stream_offset();

archive.finish().unwrap();

assert_eq!(local_header_offset, 0);
assert!(data_start_offset > local_header_offset);
assert_eq!(end_data_offset, data_start_offset + b"Hello World".len() as u64);
assert_eq!(end_descriptor_offset, end_data_offset + 16); // 16 bytes for data descriptor
assert_eq!(compressed_bytes, end_data_offset - data_start_offset);
Source§

impl<W> ZipArchiveWriter<W>
where W: Write,

Source

pub fn new_dir<'a>(&'a mut self, name: &'a str) -> ZipDirBuilder<'a, W>

Creates a builder for adding a new directory to the archive.

The name of the directory must end with a /.

§Example
archive.new_dir("my-dir/")
    .unix_permissions(0o755)
    .create()?;
Source

pub fn new_file<'name>( &mut self, name: &'name str, ) -> ZipFileBuilder<'_, 'name, W>

Creates a builder for adding a new file to the archive.

§Example
let (mut entry, config) = archive.new_file("my-file")
    .compression_method(rawzip::CompressionMethod::Deflate)
    .unix_permissions(0o644)
    .start()?;
let mut writer = config.wrap(&mut entry);
writer.write_all(b"Hello, world!")?;
let (_, output) = writer.finish()?;
entry.finish(output)?;
Source

pub fn finish(self) -> Result<W, Error>
where W: Write,

Finishes writing the archive and returns the underlying writer.

This writes the central directory and the end of central directory record. ZIP64 format is used automatically when thresholds are exceeded.

Trait Implementations§

Source§

impl<W: Debug> Debug for ZipArchiveWriter<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<W> Unpin for ZipArchiveWriter<W>
where W: Unpin,

§

impl<W> UnwindSafe for ZipArchiveWriter<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.