pub struct ArchiveWriter<W: Write> { /* private fields */ }Expand description
Wraps a writer to create a ZIP archive.
You need to call self.finish() when done.
When adding a file to the archive, some checks are made to ensure its name is valid (it is not absolute, does not contain the ‘\’ character, etc). Such validation checks may be added in a semver-compatible version if they may prevent invalid or dangerous archives.
§Example
use std::io::prelude::*;
let mut archive = eazip::ArchiveWriter::create("example.zip")?;
let options = eazip::write::FileOptions::default();
// Add a file
archive.add_file("hello.txt", b"hello\n".as_slice(), &options)?;
// Add a directory
archive.add_directory("dir/")?;
// Stream a file
let mut writer = archive.stream_file("dir/streaming.txt", &options)?;
writer.write_all(b"some data\n")?;
writer.finish()?;
// Finish writing the archive
archive.finish()?;Implementations§
Source§impl ArchiveWriter<File>
impl ArchiveWriter<File>
Sourcepub fn create(path: impl AsRef<Path>) -> Result<Self>
pub fn create(path: impl AsRef<Path>) -> Result<Self>
Creates a new ArchiveWriter that writes to the given file.
The file will be created if it does not exist, and will be truncated if it does.
Sourcepub fn create_new(path: impl AsRef<Path>) -> Result<Self>
pub fn create_new(path: impl AsRef<Path>) -> Result<Self>
Creates a new ArchiveWriter that writes to the given file; error if
the file exists.
Source§impl<W: Write> ArchiveWriter<W>
impl<W: Write> ArchiveWriter<W>
Sourcepub fn add_file<R: Read>(
&mut self,
name: &str,
content: R,
options: &FileOptions,
) -> Result<()>
pub fn add_file<R: Read>( &mut self, name: &str, content: R, options: &FileOptions, ) -> Result<()>
Writes a file to the archive.
The entire compressed content of the file must fit in memory.
Sourcepub fn stream_file(
&mut self,
name: &str,
options: &FileOptions,
) -> Result<FileStreamer<'_, W>>
pub fn stream_file( &mut self, name: &str, options: &FileOptions, ) -> Result<FileStreamer<'_, W>>
Starts streaming a file to the archive.
This is useful for (but not limited to) very large files that may not fit in memory.
This method returns a FileStreamer that can be written to.
Sourcepub fn add_directory(&mut self, name: &str) -> Result<()>
pub fn add_directory(&mut self, name: &str) -> Result<()>
Adds a directory to the archive.
Sourcepub fn add_symlink(&mut self, name: &str, target: &str) -> Result<()>
pub fn add_symlink(&mut self, name: &str, target: &str) -> Result<()>
Adds a symlink to the archive.
The target of the symlink is not validated yet, which may be used to create dangerous archives if used with untrusted input. This will be fixed in a future version so this behaviour should not be relied on.
Sourcepub fn recover(&mut self) -> Result<()>where
W: Seek,
pub fn recover(&mut self) -> Result<()>where
W: Seek,
Tries to recover from an error by erasing the last entry.
Note that this requires a seeking writer. Calling this when no error needs recovery does nothing.
Footgun: this requires the user to properly truncate the writer after using this method.