Struct zip::write::ZipWriter[][src]

pub struct ZipWriter<W: Write + Seek> { /* fields omitted */ }

ZIP archive generator

Handles the bookkeeping involved in building an archive, and provides an API to edit its contents.

use std::io::Write;
use zip::write::FileOptions;

// We use a buffer here, though you'd normally use a `File`
let mut buf = [0; 65536];
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(&mut buf[..]));

let options = zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
zip.start_file("hello_world.txt", options)?;
zip.write(b"Hello, World!")?;

// Apply the changes you've made.
// Dropping the `ZipWriter` will have the same effect, but may silently fail
zip.finish()?;

Implementations

impl<W: Write + Seek> ZipWriter<W>[src]

pub fn new(inner: W) -> ZipWriter<W>

Notable traits for ZipWriter<W>

impl<W: Write + Seek> Write for ZipWriter<W>
[src]

Initializes the archive.

Before writing to this object, the ZipWriter::start_file function should be called.

pub fn set_comment<S>(&mut self, comment: S) where
    S: Into<String>, 
[src]

Set ZIP archive comment.

pub fn start_file<S>(&mut self, name: S, options: FileOptions) -> ZipResult<()> where
    S: Into<String>, 
[src]

Create a file in the archive and start writing its’ contents.

The data should be written using the io::Write implementation on this ZipWriter

pub fn start_file_from_path(
    &mut self,
    path: &Path,
    options: FileOptions
) -> ZipResult<()>
[src]

👎 Deprecated since 0.5.7:

by stripping ..s from the path, the meaning of paths can change. Use start_file instead.

Starts a file, taking a Path as argument.

This function ensures that the ‘/’ path seperator is used. It also ignores all non ‘Normal’ Components, such as a starting ‘/’ or ‘..’ and ‘.’.

pub fn raw_copy_file_rename<S>(
    &mut self,
    file: ZipFile<'_>,
    name: S
) -> ZipResult<()> where
    S: Into<String>, 
[src]

Add a new file using the already compressed data from a ZIP file being read and renames it, this allows faster copies of the ZipFile since there is no need to decompress and compress it again. Any ZipFile metadata is copied and not checked, for example the file CRC.

use std::fs::File;
use std::io::{Read, Seek, Write};
use zip::{ZipArchive, ZipWriter};

fn copy_rename<R, W>(
    src: &mut ZipArchive<R>,
    dst: &mut ZipWriter<W>,
) -> zip::result::ZipResult<()>
where
    R: Read + Seek,
    W: Write + Seek,
{
    // Retrieve file entry by name
    let file = src.by_name("src_file.txt")?;

    // Copy and rename the previously obtained file entry to the destination zip archive
    dst.raw_copy_file_rename(file, "new_name.txt")?;

    Ok(())
}

pub fn raw_copy_file(&mut self, file: ZipFile<'_>) -> ZipResult<()>[src]

Add a new file using the already compressed data from a ZIP file being read, this allows faster copies of the ZipFile since there is no need to decompress and compress it again. Any ZipFile metadata is copied and not checked, for example the file CRC.

use std::fs::File;
use std::io::{Read, Seek, Write};
use zip::{ZipArchive, ZipWriter};

fn copy<R, W>(src: &mut ZipArchive<R>, dst: &mut ZipWriter<W>) -> zip::result::ZipResult<()>
where
    R: Read + Seek,
    W: Write + Seek,
{
    // Retrieve file entry by name
    let file = src.by_name("src_file.txt")?;

    // Copy the previously obtained file entry to the destination zip archive
    dst.raw_copy_file(file)?;

    Ok(())
}

pub fn add_directory<S>(
    &mut self,
    name: S,
    options: FileOptions
) -> ZipResult<()> where
    S: Into<String>, 
[src]

Add a directory entry.

You can’t write data to the file afterwards.

pub fn add_directory_from_path(
    &mut self,
    path: &Path,
    options: FileOptions
) -> ZipResult<()>
[src]

👎 Deprecated since 0.5.7:

by stripping ..s from the path, the meaning of paths can change. Use add_directory instead.

Add a directory entry, taking a Path as argument.

This function ensures that the ‘/’ path seperator is used. It also ignores all non ‘Normal’ Components, such as a starting ‘/’ or ‘..’ and ‘.’.

pub fn finish(&mut self) -> ZipResult<W>[src]

Finish the last file and write all other zip-structures

This will return the writer, but one should normally not append any data to the end of the file. Note that the zipfile will also be finished on drop.

Trait Implementations

impl<W: Write + Seek> Drop for ZipWriter<W>[src]

impl<W: Write + Seek> Write for ZipWriter<W>[src]

Auto Trait Implementations

impl<W> RefUnwindSafe for ZipWriter<W> where
    W: RefUnwindSafe

impl<W> Send for ZipWriter<W> where
    W: Send

impl<W> Sync for ZipWriter<W> where
    W: Sync

impl<W> Unpin for ZipWriter<W> where
    W: Unpin

impl<W> UnwindSafe for ZipWriter<W> where
    W: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<W> WriteBytesExt for W where
    W: Write + ?Sized
[src]