Do not switch compression if not needed

This commit is contained in:
Mathijs van de Nes 2015-03-17 17:22:30 +01:00
parent 0339ed2742
commit aa8acab465
2 changed files with 17 additions and 2 deletions

View file

@ -1,7 +1,7 @@
//! Possible ZIP compression methods.
/// Compression methods for the contents of a ZIP file.
#[derive(Copy)]
#[derive(Copy, PartialEq)]
pub enum CompressionMethod
{
/// The file is stored (no compression)

View file

@ -231,6 +231,12 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
{
fn switch_to(&mut self, compression: CompressionMethod) -> ZipResult<()>
{
match self.current_compression() {
Some(method) if method == compression => return Ok(()),
None => try!(Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed", None))),
_ => {},
}
let bare = match mem::replace(self, GenericZipWriter::Closed)
{
GenericZipWriter::Storer(w) => w,
@ -244,7 +250,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
CompressionMethod::Stored => GenericZipWriter::Storer(bare),
CompressionMethod::Deflated => GenericZipWriter::Deflater(bare.deflate_encode(flate2::Compression::Default)),
CompressionMethod::Bzip2 => GenericZipWriter::Bzip2(BzCompressor::new(bare, bzip2::Compress::Default)),
_ => return Err(ZipError::UnsupportedArchive("Unsupported compression")),
CompressionMethod::Unsupported(..) => return Err(ZipError::UnsupportedArchive("Unsupported compression")),
};
Ok(())
@ -277,6 +283,15 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
}
}
fn current_compression(&self) -> Option<CompressionMethod> {
match *self {
GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored),
GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated),
GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2),
GenericZipWriter::Closed => None,
}
}
fn unwrap(self) -> W
{
match self