Struct zip::write::ZipWriter [−][src]
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>ⓘ
[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]
S: Into<String>,
Set ZIP archive comment.
pub fn start_file<S>(&mut self, name: S, options: FileOptions) -> ZipResult<()> where
S: Into<String>,
[src]
S: Into<String>,
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]
&mut self,
path: &Path,
options: FileOptions
) -> ZipResult<()>
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]
&mut self,
file: ZipFile<'_>,
name: S
) -> ZipResult<()> where
S: Into<String>,
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]
&mut self,
name: S,
options: FileOptions
) -> ZipResult<()> where
S: Into<String>,
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]
&mut self,
path: &Path,
options: FileOptions
) -> ZipResult<()>
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]
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn flush(&mut self) -> Result<()>
[src]
pub fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
1.36.0[src]
pub fn is_write_vectored(&self) -> bool
[src]
pub fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
pub fn write_all_vectored(
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
[src]
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
1.0.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl<W> RefUnwindSafe for ZipWriter<W> where
W: RefUnwindSafe,
W: RefUnwindSafe,
impl<W> Send for ZipWriter<W> where
W: Send,
W: Send,
impl<W> Sync for ZipWriter<W> where
W: Sync,
W: Sync,
impl<W> Unpin for ZipWriter<W> where
W: Unpin,
W: Unpin,
impl<W> UnwindSafe for ZipWriter<W> where
W: UnwindSafe,
W: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<W> WriteBytesExt for W where
W: Write + ?Sized,
[src]
W: Write + ?Sized,
pub fn write_u8(&mut self, n: u8) -> Result<(), Error>
[src]
pub fn write_i8(&mut self, n: i8) -> Result<(), Error>
[src]
pub fn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_u24<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_i24<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_u48<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_i48<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_u128<T>(&mut self, n: u128) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_i128<T>(&mut self, n: i128) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_f32<T>(&mut self, n: f32) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,
pub fn write_f64<T>(&mut self, n: f64) -> Result<(), Error> where
T: ByteOrder,
[src]
T: ByteOrder,