diff --git a/src/compression.rs b/src/compression.rs index a70dfcd7..ebe653be 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -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) diff --git a/src/write.rs b/src/write.rs index 923e7804..47022f7d 100644 --- a/src/write.rs +++ b/src/write.rs @@ -231,6 +231,12 @@ impl GenericZipWriter { 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 GenericZipWriter 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 GenericZipWriter } } + fn current_compression(&self) -> Option { + 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