Struct otter_api_tests::zipfile::write::ZipWriter
[−]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
impl<A> ZipWriter<A> where
A: Read + Write + Seek,
impl<A> ZipWriter<A> where
A: Read + Write + Seek,
pub fn new_append(readwriter: A) -> Result<ZipWriter<A>, ZipError>
pub fn new_append(readwriter: A) -> Result<ZipWriter<A>, ZipError>
Initializes the archive from an existing ZIP archive, making it ready for append.
impl<W> ZipWriter<W> where
W: Write + Seek,
impl<W> ZipWriter<W> where
W: Write + Seek,
pub fn new(inner: W) -> ZipWriter<W>ⓘNotable traits for ZipWriter<W>impl<W> Write for ZipWriter<W> where
W: Write + Seek,
pub fn new(inner: W) -> ZipWriter<W>ⓘNotable traits for ZipWriter<W>impl<W> Write for ZipWriter<W> where
W: Write + Seek,
W: Write + Seek,
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>,
pub fn set_comment<S>(&mut self, comment: S) where
S: Into<String>,
Set ZIP archive comment.
pub fn set_raw_comment(&mut self, comment: Vec<u8, Global>)
pub fn set_raw_comment(&mut self, comment: Vec<u8, Global>)
Set ZIP archive comment.
This sets the raw bytes of the comment. The comment is typically expected to be encoded in UTF-8
pub fn start_file<S>(
&mut self,
name: S,
options: FileOptions
) -> Result<(), ZipError> where
S: Into<String>,
pub fn start_file<S>(
&mut self,
name: S,
options: FileOptions
) -> Result<(), ZipError> where
S: Into<String>,
pub fn start_file_from_path(
&mut self,
path: &Path,
options: FileOptions
) -> Result<(), ZipError>
👎 Deprecated since 0.5.7: by stripping ..
s from the path, the meaning of paths can change. Use start_file
instead.
pub fn start_file_from_path(
&mut self,
path: &Path,
options: FileOptions
) -> Result<(), ZipError>
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 ‘.’.
pub fn start_file_aligned<S>(
&mut self,
name: S,
options: FileOptions,
align: u16
) -> Result<u64, ZipError> where
S: Into<String>,
pub fn start_file_aligned<S>(
&mut self,
name: S,
options: FileOptions,
align: u16
) -> Result<u64, ZipError> where
S: Into<String>,
pub fn start_file_with_extra_data<S>(
&mut self,
name: S,
options: FileOptions
) -> Result<u64, ZipError> where
S: Into<String>,
pub fn start_file_with_extra_data<S>(
&mut self,
name: S,
options: FileOptions
) -> Result<u64, ZipError> where
S: Into<String>,
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");
pub fn end_local_start_central_extra_data(&mut self) -> Result<u64, ZipError>
pub fn end_local_start_central_extra_data(&mut self) -> Result<u64, ZipError>
End local and start central extra data. Requires ZipWriter::start_file_with_extra_data
.
Returns the final starting offset of the file data.
pub fn end_extra_data(&mut self) -> Result<u64, ZipError>
pub fn end_extra_data(&mut self) -> Result<u64, ZipError>
End extra data and start file data. Requires ZipWriter::start_file_with_extra_data
.
Returns the final starting offset of the file data.
pub fn raw_copy_file_rename<S>(
&mut self,
file: ZipFile<'_>,
name: S
) -> Result<(), ZipError> where
S: Into<String>,
pub fn raw_copy_file_rename<S>(
&mut self,
file: ZipFile<'_>,
name: S
) -> Result<(), ZipError> 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<'_>) -> Result<(), ZipError>
pub fn raw_copy_file(&mut self, file: ZipFile<'_>) -> Result<(), ZipError>
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
) -> Result<(), ZipError> where
S: Into<String>,
pub fn add_directory<S>(
&mut self,
name: S,
options: FileOptions
) -> Result<(), ZipError> 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
) -> Result<(), ZipError>
👎 Deprecated since 0.5.7: by stripping ..
s from the path, the meaning of paths can change. Use add_directory
instead.
pub fn add_directory_from_path(
&mut self,
path: &Path,
options: FileOptions
) -> Result<(), ZipError>
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 ‘.’.
Trait Implementations
impl<W> Write for ZipWriter<W> where
W: Write + Seek,
impl<W> Write for ZipWriter<W> where
W: Write + Seek,
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Write a buffer into this writer, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
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>
impl<W> Unpin for ZipWriter<W> where
W: Unpin,
impl<W> UnwindSafe for ZipWriter<W> where
W: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
impl<A> DynCastExt for A
impl<A> DynCastExt for A
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
Use this to cast from one trait object type to another. Read more
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
Use this to upcast a trait to one of its supertraits. Read more
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
impl<T> RmpWrite for T where
T: Write,
impl<T> RmpWrite for T where
T: Write,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
impl<W> WriteBytesExt for W where
W: Write + ?Sized,
impl<W> WriteBytesExt for W where
W: Write + ?Sized,
fn write_u8(&mut self, n: u8) -> Result<(), Error>
fn write_u8(&mut self, n: u8) -> Result<(), Error>
Writes an unsigned 8 bit integer to the underlying writer. Read more
fn write_i8(&mut self, n: i8) -> Result<(), Error>
fn write_i8(&mut self, n: i8) -> Result<(), Error>
Writes a signed 8 bit integer to the underlying writer. Read more
fn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
T: ByteOrder,
fn write_u16<T>(&mut self, n: u16) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 16 bit integer to the underlying writer. Read more
fn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
T: ByteOrder,
fn write_i16<T>(&mut self, n: i16) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 16 bit integer to the underlying writer. Read more
fn write_u24<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
fn write_u24<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 24 bit integer to the underlying writer. Read more
fn write_i24<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
fn write_i24<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 24 bit integer to the underlying writer. Read more
fn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
fn write_u32<T>(&mut self, n: u32) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 32 bit integer to the underlying writer. Read more
fn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
fn write_i32<T>(&mut self, n: i32) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 32 bit integer to the underlying writer. Read more
fn write_u48<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
fn write_u48<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 48 bit integer to the underlying writer. Read more
fn write_i48<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
fn write_i48<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 48 bit integer to the underlying writer. Read more
fn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
fn write_u64<T>(&mut self, n: u64) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 64 bit integer to the underlying writer. Read more
fn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
fn write_i64<T>(&mut self, n: i64) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 64 bit integer to the underlying writer. Read more
fn write_u128<T>(&mut self, n: u128) -> Result<(), Error> where
T: ByteOrder,
fn write_u128<T>(&mut self, n: u128) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned 128 bit integer to the underlying writer.
fn write_i128<T>(&mut self, n: i128) -> Result<(), Error> where
T: ByteOrder,
fn write_i128<T>(&mut self, n: i128) -> Result<(), Error> where
T: ByteOrder,
Writes a signed 128 bit integer to the underlying writer.
fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_uint<T>(&mut self, n: u64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned n-bytes integer to the underlying writer. Read more
fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_int<T>(&mut self, n: i64, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes a signed n-bytes integer to the underlying writer. Read more
fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_uint128<T>(&mut self, n: u128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes an unsigned n-bytes integer to the underlying writer. Read more
fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
fn write_int128<T>(&mut self, n: i128, nbytes: usize) -> Result<(), Error> where
T: ByteOrder,
Writes a signed n-bytes integer to the underlying writer. Read more