ArchiveWriter

Struct ArchiveWriter 

Source
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>

Source

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.

Source

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>

Source

pub fn new(writer: W) -> Self

Creates a new ArchiveWriter that writes to the given writer.

Source

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.

Source

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.

Source

pub fn add_directory(&mut self, name: &str) -> Result<()>

Adds a directory to the archive.

Adds a symlink to the archive.

Source

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.

Source

pub fn get_ref(&self) -> &W

Gets a shared reference to the underlying writer.

Source

pub fn get_mut(&mut self) -> &mut W

Gets a mutable reference to the underlying writer.

It is inadvisable to directly write to the underlying writer.

Source

pub fn flush(&mut self) -> Result<()>

Flushes the underlying stream.

Source

pub fn finish(self) -> Result<W>

Finishes writing the archive and get the writer back.

It is necessary to call this method or the resulting archive will not be readable.

Trait Implementations§

Source§

impl<W: Default + Write> Default for ArchiveWriter<W>

Source§

fn default() -> ArchiveWriter<W>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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