diff --git a/src/aes.rs b/src/aes.rs index 726d70aed..eb73d14f9 100644 --- a/src/aes.rs +++ b/src/aes.rs @@ -264,6 +264,16 @@ impl AesWriter { }) } + /// 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 + } + pub fn finish(mut self) -> io::Result { self.write_encrypted_file_header()?; diff --git a/src/write.rs b/src/write.rs index 64e85a4de..0c0fdb438 100644 --- a/src/write.rs +++ b/src/write.rs @@ -53,6 +53,25 @@ enum MaybeEncrypted { ZipCrypto(crate::zipcrypto::ZipCryptoWriter), } +impl MaybeEncrypted { + 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(), + } + } + fn get_mut(&mut self) -> &mut W { + match self { + MaybeEncrypted::Unencrypted(w) => w, + #[cfg(feature = "aes-crypto")] + MaybeEncrypted::Aes(w) => w.get_mut(), + MaybeEncrypted::ZipCrypto(w) => w.get_mut(), + } + } +} + impl Debug for MaybeEncrypted { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { // Don't print W, since it may be a huge Vec @@ -182,6 +201,55 @@ pub(crate) mod zip_writer { self.comment, self.flush_on_finish_file)) } } + + impl ZipWriter { + /// Gets a reference to the underlying writer in this ZipWrite. + pub fn get_ref(&self) -> &W { + use GenericZipWriter::*; + match &self.inner { + Closed => panic!("Can't get underlying writer of closed archive"), + Storer(w) => w.get_ref(), + #[cfg(feature = "deflate-flate2")] + Deflater(w) => w.get_ref().get_ref(), + #[cfg(feature = "deflate-zopfli")] + ZopfliDeflater(w) => w.get_ref().get_ref(), + #[cfg(feature = "deflate-zopfli")] + BufferedZopfliDeflater(w) => w.get_ref().get_ref().get_ref(), + #[cfg(feature = "bzip2")] + Bzip2(w) => w.get_ref().get_ref(), + #[cfg(feature = "zstd")] + Zstd(w) => w.get_ref().get_ref(), + #[cfg(feature = "xz")] + Xz(w) => w.inner().get_ref(), + #[cfg(feature = "ppmd")] + Ppmd(w) => w.get_ref().get_ref(), + } + } + + /// Gets a reference to the underlying writer in this ZipWrite. + pub fn get_mut(&mut self) -> &mut W { + use MaybeEncrypted::*; + use GenericZipWriter::*; + match &mut self.inner { + Closed => panic!("Can't get underlying writer of closed archive"), + Storer(w) => w.get_mut(), + #[cfg(feature = "deflate-flate2")] + Deflater(w) => w.get_mut().get_mut(), + #[cfg(feature = "deflate-zopfli")] + ZopfliDeflater(w) => w.get_mut().get_mut(), + #[cfg(feature = "deflate-zopfli")] + BufferedZopfliDeflater(w) => w.get_mut().get_mut().get_mut(), + #[cfg(feature = "bzip2")] + Bzip2(w) => w.get_mut().get_mut(), + #[cfg(feature = "zstd")] + Zstd(w) => w.get_mut().get_mut(), + #[cfg(feature = "xz")] + Xz(w) => w.inner_mut().get_mut(), + #[cfg(feature = "ppmd")] + Ppmd(w) => w.get_mut().get_mut(), + } + } + } } #[doc(inline)] pub use self::sealed::FileOptionExtension; @@ -2142,6 +2210,16 @@ impl StreamWriter { } } + /// 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 diff --git a/src/zipcrypto.rs b/src/zipcrypto.rs index 7ad017c8c..3d344778a 100644 --- a/src/zipcrypto.rs +++ b/src/zipcrypto.rs @@ -163,6 +163,16 @@ pub(crate) struct ZipCryptoWriter { pub(crate) keys: ZipCryptoKeys, } impl ZipCryptoWriter { + /// 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, crc32: u32) -> std::io::Result { self.buffer[11] = (crc32 >> 24) as u8;