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 usualImplementations§
Source§impl ZipArchiveWriter<()>
impl ZipArchiveWriter<()>
Sourcepub fn builder() -> ZipArchiveWriterBuilder
pub fn builder() -> ZipArchiveWriterBuilder
Creates a ZipArchiveWriterBuilder for configuring the writer.
Source§impl<W> ZipArchiveWriter<W>
impl<W> ZipArchiveWriter<W>
Sourcepub fn stream_offset(&self) -> u64
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,
impl<W> ZipArchiveWriter<W>where
W: Write,
Sourcepub fn new_dir<'a>(&'a mut self, name: &'a str) -> ZipDirBuilder<'a, W>
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()?;Sourcepub fn new_file<'name>(
&mut self,
name: &'name str,
) -> ZipFileBuilder<'_, 'name, W>
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)?;Trait Implementations§
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> UnsafeUnpin for ZipArchiveWriter<W>where
W: UnsafeUnpin,
impl<W> UnwindSafe for ZipArchiveWriter<W>where
W: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more