This commit is contained in:
Chris Hennick 2023-05-04 10:21:15 -07:00
parent d5e45f3c20
commit 304becbda3
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74

View file

@ -469,7 +469,9 @@ impl<W: Write + Seek> ZipWriter<W> {
// Implicitly calling [`ZipWriter::end_extra_data`] for empty files. // Implicitly calling [`ZipWriter::end_extra_data`] for empty files.
self.end_extra_data()?; self.end_extra_data()?;
} }
let make_plain_writer = self.inner.prepare_switch_to(CompressionMethod::Stored, None)?; let make_plain_writer = self
.inner
.prepare_switch_to(CompressionMethod::Stored, None)?;
self.inner.switch_to(make_plain_writer)?; self.inner.switch_to(make_plain_writer)?;
let writer = self.inner.get_plain(); let writer = self.inner.get_plain();
@ -498,8 +500,9 @@ impl<W: Write + Seek> ZipWriter<W> {
pub fn abort_file(&mut self) -> ZipResult<()> { pub fn abort_file(&mut self) -> ZipResult<()> {
self.files_by_name self.files_by_name
.remove(&*self.files.pop().unwrap().file_name); .remove(&*self.files.pop().unwrap().file_name);
let make_plain_writer let make_plain_writer = self
= self.inner.prepare_switch_to(CompressionMethod::Stored, None)?; .inner
.prepare_switch_to(CompressionMethod::Stored, None)?;
self.inner.switch_to(make_plain_writer)?; self.inner.switch_to(make_plain_writer)?;
self.writing_to_file = false; self.writing_to_file = false;
self.writing_raw = false; self.writing_raw = false;
@ -515,7 +518,8 @@ impl<W: Write + Seek> ZipWriter<W> {
S: Into<String>, S: Into<String>,
{ {
Self::normalize_options(&mut options); Self::normalize_options(&mut options);
let make_new_self = self.inner let make_new_self = self
.inner
.prepare_switch_to(options.compression_method, options.compression_level)?; .prepare_switch_to(options.compression_method, options.compression_level)?;
self.start_entry(name, options, None)?; self.start_entry(name, options, None)?;
self.inner.switch_to(make_new_self)?; self.inner.switch_to(make_new_self)?;
@ -685,8 +689,10 @@ impl<W: Write + Seek> ZipWriter<W> {
return Err(e); return Err(e);
} }
let make_compressing_writer = match self.inner let make_compressing_writer = match self
.prepare_switch_to(file.compression_method, file.compression_level) { .inner
.prepare_switch_to(file.compression_method, file.compression_level)
{
Ok(writer) => writer, Ok(writer) => writer,
Err(e) => { Err(e) => {
let _ = self.abort_file(); let _ = self.abort_file();
@ -1002,18 +1008,18 @@ impl<W: Write + Seek> GenericZipWriter<W> {
} }
} }
#[cfg(any( #[cfg(any(
feature = "deflate", feature = "deflate",
feature = "deflate-miniz", feature = "deflate-miniz",
feature = "deflate-zlib" feature = "deflate-zlib"
))] ))]
CompressionMethod::Deflated => { CompressionMethod::Deflated => {
let level = clamp_opt( let level = clamp_opt(
compression_level.unwrap_or(flate2::Compression::default().level() as i32), compression_level.unwrap_or(flate2::Compression::default().level() as i32),
deflate_compression_level_range(), deflate_compression_level_range(),
) )
.ok_or(ZipError::UnsupportedArchive( .ok_or(ZipError::UnsupportedArchive(
"Unsupported compression level", "Unsupported compression level",
))? as u32; ))? as u32;
Ok(Box::new(move |bare| { Ok(Box::new(move |bare| {
GenericZipWriter::Deflater(DeflateEncoder::new( GenericZipWriter::Deflater(DeflateEncoder::new(
bare, bare,
@ -1027,9 +1033,9 @@ impl<W: Write + Seek> GenericZipWriter<W> {
compression_level.unwrap_or(bzip2::Compression::default().level() as i32), compression_level.unwrap_or(bzip2::Compression::default().level() as i32),
bzip2_compression_level_range(), bzip2_compression_level_range(),
) )
.ok_or(ZipError::UnsupportedArchive( .ok_or(ZipError::UnsupportedArchive(
"Unsupported compression level", "Unsupported compression level",
))? as u32; ))? as u32;
Ok(Box::new(move |bare| { Ok(Box::new(move |bare| {
GenericZipWriter::Bzip2(BzEncoder::new( GenericZipWriter::Bzip2(BzEncoder::new(
bare, bare,
@ -1037,20 +1043,18 @@ impl<W: Write + Seek> GenericZipWriter<W> {
)) ))
})) }))
} }
CompressionMethod::AES => { CompressionMethod::AES => Err(ZipError::UnsupportedArchive(
Err(ZipError::UnsupportedArchive( "AES compression is not supported for writing",
"AES compression is not supported for writing", )),
))
}
#[cfg(feature = "zstd")] #[cfg(feature = "zstd")]
CompressionMethod::Zstd => { CompressionMethod::Zstd => {
let level = clamp_opt( let level = clamp_opt(
compression_level.unwrap_or(zstd::DEFAULT_COMPRESSION_LEVEL), compression_level.unwrap_or(zstd::DEFAULT_COMPRESSION_LEVEL),
zstd::compression_level_range(), zstd::compression_level_range(),
) )
.ok_or(ZipError::UnsupportedArchive( .ok_or(ZipError::UnsupportedArchive(
"Unsupported compression level", "Unsupported compression level",
))?; ))?;
Ok(Box::new(move |bare| { Ok(Box::new(move |bare| {
GenericZipWriter::Zstd(ZstdEncoder::new(bare, level).unwrap()) GenericZipWriter::Zstd(ZstdEncoder::new(bare, level).unwrap())
})) }))
@ -1062,8 +1066,10 @@ impl<W: Write + Seek> GenericZipWriter<W> {
} }
} }
fn switch_to(&mut self, make_new_self: Box<dyn FnOnce(W) -> GenericZipWriter<W>>) fn switch_to(
-> ZipResult<()>{ &mut self,
make_new_self: Box<dyn FnOnce(W) -> GenericZipWriter<W>>,
) -> ZipResult<()> {
let bare = match mem::replace(self, Closed) { let bare = match mem::replace(self, Closed) {
Storer(w) => w, Storer(w) => w,
#[cfg(any( #[cfg(any(
@ -1080,7 +1086,8 @@ impl<W: Write + Seek> GenericZipWriter<W> {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::BrokenPipe, io::ErrorKind::BrokenPipe,
"ZipWriter was already closed", "ZipWriter was already closed",
).into()); )
.into());
} }
}; };
*self = (make_new_self)(bare); *self = (make_new_self)(bare);