Struct binstall_zip::ZipWriter

source ·
pub struct ZipWriter<W: Write + Seek> { /* private fields */ }
Expand description

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

Initializes the archive from an existing ZIP archive, making it ready for append.

Initializes the archive.

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

Set ZIP archive comment.

Set ZIP archive comment.

This sets the raw bytes of the comment. The comment is typically expected to be encoded in UTF-8

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

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

👎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 separator is used. It also ignores all non ‘Normal’ Components, such as a starting ‘/’ or ‘..’ and ‘.’.

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

Returns the number of padding bytes required to align the file.

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

Create a file in the archive and start writing its extra data first.

Finish writing extra data and start writing file data with ZipWriter::end_extra_data. Optionally, distinguish local from central extra data with ZipWriter::end_local_start_central_extra_data.

Returns the preliminary starting offset of the file data without any extra data allowing to align the file data by calculating a pad length to be prepended as part of the extra data.

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

use byteorder::{LittleEndian, WriteBytesExt};
use zip::{ZipArchive, ZipWriter, result::ZipResult};
use zip::{write::FileOptions, CompressionMethod};
use std::io::{Write, Cursor};

let mut archive = Cursor::new(Vec::new());

{
    let mut zip = ZipWriter::new(&mut archive);
    let options = FileOptions::default()
        .compression_method(CompressionMethod::Stored);

    zip.start_file_with_extra_data("identical_extra_data.txt", options)?;
    let extra_data = b"local and central extra data";
    zip.write_u16::<LittleEndian>(0xbeef)?;
    zip.write_u16::<LittleEndian>(extra_data.len() as u16)?;
    zip.write_all(extra_data)?;
    zip.end_extra_data()?;
    zip.write_all(b"file data")?;

    let data_start = zip.start_file_with_extra_data("different_extra_data.txt", options)?;
    let extra_data = b"local extra data";
    zip.write_u16::<LittleEndian>(0xbeef)?;
    zip.write_u16::<LittleEndian>(extra_data.len() as u16)?;
    zip.write_all(extra_data)?;
    let data_start = data_start as usize + 4 + extra_data.len() + 4;
    let align = 64;
    let pad_length = (align - data_start % align) % align;
    assert_eq!(pad_length, 19);
    zip.write_u16::<LittleEndian>(0xdead)?;
    zip.write_u16::<LittleEndian>(pad_length as u16)?;
    zip.write_all(&vec![0; pad_length])?;
    let data_start = zip.end_local_start_central_extra_data()?;
    assert_eq!(data_start as usize % align, 0);
    let extra_data = b"central extra data";
    zip.write_u16::<LittleEndian>(0xbeef)?;
    zip.write_u16::<LittleEndian>(extra_data.len() as u16)?;
    zip.write_all(extra_data)?;
    zip.end_extra_data()?;
    zip.write_all(b"file data")?;

    zip.finish()?;
}

let mut zip = ZipArchive::new(archive)?;
assert_eq!(&zip.by_index(0)?.extra_data()[4..], b"local and central extra data");
assert_eq!(&zip.by_index(1)?.extra_data()[4..], b"central extra data");

End local and start central extra data. Requires ZipWriter::start_file_with_extra_data.

Returns the final starting offset of the file data.

End extra data and start file data. Requires ZipWriter::start_file_with_extra_data.

Returns the final starting offset of the file data.

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(())
}

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(())
}

Add a directory entry.

You can’t write data to the file afterwards.

👎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 separator is used. It also ignores all non ‘Normal’ Components, such as a starting ‘/’ or ‘..’ and ‘.’.

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.

Add a symlink entry.

The zip archive will contain an entry for path name which is a symlink to target.

No validation or normalization of the paths is performed. For best results, callers should normalize \ to / and ensure symlinks are relative to other paths within the zip archive.

WARNING: not all zip implementations preserve symlinks on extract. Some zip implementations may materialize a symlink as a regular file, possibly with the content incorrectly set to the symlink target. For maximum portability, consider storing a regular file instead.

Trait Implementations

Executes the destructor for this type. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Writes an unsigned 8 bit integer to the underlying writer. Read more
Writes a signed 8 bit integer to the underlying writer. Read more
Writes an unsigned 16 bit integer to the underlying writer. Read more
Writes a signed 16 bit integer to the underlying writer. Read more
Writes an unsigned 24 bit integer to the underlying writer. Read more
Writes a signed 24 bit integer to the underlying writer. Read more
Writes an unsigned 32 bit integer to the underlying writer. Read more
Writes a signed 32 bit integer to the underlying writer. Read more
Writes an unsigned 48 bit integer to the underlying writer. Read more
Writes a signed 48 bit integer to the underlying writer. Read more
Writes an unsigned 64 bit integer to the underlying writer. Read more
Writes a signed 64 bit integer to the underlying writer. Read more
Writes an unsigned 128 bit integer to the underlying writer.
Writes a signed 128 bit integer to the underlying writer.
Writes an unsigned n-bytes integer to the underlying writer. Read more
Writes a signed n-bytes integer to the underlying writer. Read more
Writes an unsigned n-bytes integer to the underlying writer. Read more
Writes a signed n-bytes integer to the underlying writer. Read more
Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer. Read more
Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer. Read more