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.
This type only does minimal validation on the file names for now, so untrusted input may produce dangerous ZIP archives. This will be improved in a future version.
§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.
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.