Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pbkdf2 = { version = "0.12", optional = true }
sha1 = { version = "0.10", optional = true }
zeroize = { version = "1.8", optional = true, features = ["zeroize_derive"] }
zstd = { version = "^0.13.3", optional = true, default-features = false }
zopfli = { version = "0.8", optional = true }
zopfli = { version = "^0.8.3", optional = true }
Comment thread
Pr0methean marked this conversation as resolved.
deflate64 = { version = "0.1.10", optional = true }
lzma-rust2 = { version = "^0.16.1", optional = true, default-features = false, features = ["std", "encoder", "optimization", "xz"] }
bitstream-io = { version = "4.9", optional = true }
Expand Down
12 changes: 12 additions & 0 deletions src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ impl<W: Write> AesWriter<W> {
})
}

/// Gets a reference to the underlying writer.
pub fn get_ref(&self) -> &W {
&self.writer
}

/// Gets a mutable reference to the underlying writer.
/// SAFETY: The caller must not corrupt the archive, and must leave it in the same
/// cursor position as it was when obtained.
pub unsafe fn get_mut(&mut self) -> &mut W {
&mut self.writer
}

pub fn finish(mut self) -> io::Result<W> {
self.write_encrypted_file_header()?;

Expand Down
89 changes: 84 additions & 5 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ enum MaybeEncrypted<W> {
ZipCrypto(crate::zipcrypto::ZipCryptoWriter<W>),
}

impl<W: Write> MaybeEncrypted<W> {
fn get_ref(&self) -> &W {
match self {
MaybeEncrypted::Unencrypted(w) => w,
#[cfg(feature = "aes-crypto")]
MaybeEncrypted::Aes(w) => w.get_ref(),
MaybeEncrypted::ZipCrypto(w) => w.get_ref(),
}
}
unsafe fn get_mut(&mut self) -> &mut W {
match self {
MaybeEncrypted::Unencrypted(w) => w,
#[cfg(feature = "aes-crypto")]
MaybeEncrypted::Aes(w) => unsafe { w.get_mut() },
MaybeEncrypted::ZipCrypto(w) => w.get_mut(),
}
}
}

impl<W> Debug for MaybeEncrypted<W> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
// Don't print W, since it may be a huge Vec<u8>
Expand Down Expand Up @@ -110,15 +129,13 @@ impl<W: Write + Seek> Debug for GenericZipWriter<W> {

// Put the struct declaration in a private module to convince rustdoc to display ZipWriter nicely
pub(crate) mod zip_writer {
use core::fmt::{Debug, Formatter};
use std::io::{Seek, Write};

use indexmap::IndexMap;

use crate::{
types::ZipFileData,
write::{GenericZipWriter, ZipWriterStats},
};
use core::fmt::{Debug, Formatter};
use indexmap::IndexMap;
use std::io::{Seek, Write};

/// ZIP archive generator
///
Expand Down Expand Up @@ -172,6 +189,58 @@ pub(crate) mod zip_writer {
self.comment, self.flush_on_finish_file))
}
}

impl<W: Write + Seek> ZipWriter<W> {
/// Gets a reference to the underlying writer in this ZipWrite.
pub fn get_ref(&self) -> Option<&W> {
use GenericZipWriter::*;
match &self.inner {
Closed => None,
Storer(w) => Some(w.get_ref()),
#[cfg(feature = "deflate-flate2")]
Deflater(w) => Some(w.get_ref().get_ref()),
#[cfg(feature = "deflate-zopfli")]
ZopfliDeflater(w) => Some(w.get_ref().get_ref()),
#[cfg(feature = "deflate-zopfli")]
BufferedZopfliDeflater(w) => Some(w.get_ref().get_ref().get_ref()),
#[cfg(feature = "bzip2")]
Bzip2(w) => Some(w.get_ref().get_ref()),
#[cfg(feature = "zstd")]
Zstd(w) => Some(w.get_ref().get_ref()),
#[cfg(feature = "xz")]
Xz(w) => Some(w.inner().get_ref()),
#[cfg(feature = "ppmd")]
Ppmd(w) => Some(w.get_ref().get_ref()),
}
}

/// Gets a reference to the underlying writer in this ZipWrite.
/// SAFETY: Caller must not corrupt the archive, and must seek back to the current position
/// before continuing to write to the ZipWriter.
pub unsafe fn get_mut(&mut self) -> Option<&mut W> {
use GenericZipWriter::*;
unsafe {
match &mut self.inner {
Closed => None,
Storer(w) => Some(w.get_mut()),
#[cfg(feature = "deflate-flate2")]
Deflater(w) => Some(w.get_mut().get_mut()),
#[cfg(feature = "deflate-zopfli")]
ZopfliDeflater(w) => Some(w.get_mut().get_mut()),
#[cfg(feature = "deflate-zopfli")]
BufferedZopfliDeflater(w) => Some(w.get_mut().get_mut().get_mut()),
#[cfg(feature = "bzip2")]
Bzip2(w) => Some(w.get_mut().get_mut()),
#[cfg(feature = "zstd")]
Zstd(w) => Some(w.get_mut().get_mut()),
#[cfg(feature = "xz")]
Xz(w) => Some(w.inner_mut().get_mut()),
#[cfg(feature = "ppmd")]
Ppmd(w) => Some(w.get_mut().get_mut()),
}
}
}
}
}
#[doc(inline)]
pub use self::sealed::FileOptionExtension;
Expand Down Expand Up @@ -2381,6 +2450,16 @@ impl<W: Write> StreamWriter<W> {
}
}

/// Gets a reference to the underlying writer.
pub fn get_ref(&self) -> &W {
&self.inner
}

/// Gets a mutable reference to the underlying writer.
pub fn get_mut(&mut self) -> &mut W {
&mut self.inner
}

/// Consumes this wrapper, returning the underlying writer.
pub fn into_inner(self) -> W {
self.inner
Expand Down
10 changes: 10 additions & 0 deletions src/zipcrypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ pub(crate) struct ZipCryptoWriter<W> {
pub(crate) buffer: [u8; CHUNK_SIZE],
}
impl<W: std::io::Write> ZipCryptoWriter<W> {
/// Gets a reference to the underlying writer.
pub fn get_ref(&self) -> &W {
&self.writer
}

/// Gets a mutable reference to the underlying writer.
pub fn get_mut(&mut self) -> &mut W {
&mut self.writer
}

#[allow(unused)]
pub(crate) fn finish(mut self) -> std::io::Result<W> {
Ok(self.writer)
Expand Down