Merge pull request #149 from piotrmaks/any-flate2-backend

Enable deflated compression method for any flate2 backend
This commit is contained in:
Plecra 2020-06-23 18:52:52 +01:00 committed by GitHub
commit 8a6ca63728
Signed by: DevComp
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 20 deletions

View file

@ -14,9 +14,17 @@ fn main() {
const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored); const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored);
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
const METHOD_DEFLATED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Deflated); const METHOD_DEFLATED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Deflated);
#[cfg(not(feature = "deflate"))] #[cfg(not(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
)))]
const METHOD_DEFLATED: Option<zip::CompressionMethod> = None; const METHOD_DEFLATED: Option<zip::CompressionMethod> = None;
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]

View file

@ -8,8 +8,12 @@ use std::fmt;
pub enum CompressionMethod { pub enum CompressionMethod {
/// The file is stored (no compression) /// The file is stored (no compression)
Stored, Stored,
/// Deflate in pure rust /// Deflate using any flate2 backend
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
Deflated, Deflated,
/// File is compressed using BZIP2 algorithm /// File is compressed using BZIP2 algorithm
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
@ -32,7 +36,11 @@ impl CompressionMethod {
#[allow(deprecated)] #[allow(deprecated)]
match val { match val {
0 => CompressionMethod::Stored, 0 => CompressionMethod::Stored,
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
8 => CompressionMethod::Deflated, 8 => CompressionMethod::Deflated,
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
12 => CompressionMethod::Bzip2, 12 => CompressionMethod::Bzip2,
@ -50,7 +58,11 @@ impl CompressionMethod {
#[allow(deprecated)] #[allow(deprecated)]
match self { match self {
CompressionMethod::Stored => 0, CompressionMethod::Stored => 0,
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
CompressionMethod::Deflated => 8, CompressionMethod::Deflated => 8,
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => 12, CompressionMethod::Bzip2 => 12,
@ -84,7 +96,11 @@ mod test {
fn methods() -> Vec<CompressionMethod> { fn methods() -> Vec<CompressionMethod> {
let mut methods = Vec::new(); let mut methods = Vec::new();
methods.push(CompressionMethod::Stored); methods.push(CompressionMethod::Stored);
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
methods.push(CompressionMethod::Deflated); methods.push(CompressionMethod::Deflated);
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
methods.push(CompressionMethod::Bzip2); methods.push(CompressionMethod::Bzip2);

View file

@ -13,7 +13,11 @@ use crate::cp437::FromCp437;
use crate::types::{DateTime, System, ZipFileData}; use crate::types::{DateTime, System, ZipFileData};
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
use flate2::read::DeflateDecoder; use flate2::read::DeflateDecoder;
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
@ -279,7 +283,11 @@ impl<R: Read + io::Seek> ZipArchive<R> {
enum ZipFileReader<'a> { enum ZipFileReader<'a> {
NoReader, NoReader,
Stored(Crc32Reader<io::Take<&'a mut dyn Read>>), Stored(Crc32Reader<io::Take<&'a mut dyn Read>>),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
Deflated(Crc32Reader<flate2::read::DeflateDecoder<io::Take<&'a mut dyn Read>>>), Deflated(Crc32Reader<flate2::read::DeflateDecoder<io::Take<&'a mut dyn Read>>>),
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
Bzip2(Crc32Reader<BzDecoder<io::Take<&'a mut dyn Read>>>), Bzip2(Crc32Reader<BzDecoder<io::Take<&'a mut dyn Read>>>),
@ -296,7 +304,11 @@ fn make_reader<'a>(
) -> ZipResult<ZipFileReader<'a>> { ) -> ZipResult<ZipFileReader<'a>> {
match compression_method { match compression_method {
CompressionMethod::Stored => Ok(ZipFileReader::Stored(Crc32Reader::new(reader, crc32))), CompressionMethod::Stored => Ok(ZipFileReader::Stored(Crc32Reader::new(reader, crc32))),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
CompressionMethod::Deflated => { CompressionMethod::Deflated => {
let deflate_reader = DeflateDecoder::new(reader); let deflate_reader = DeflateDecoder::new(reader);
Ok(ZipFileReader::Deflated(Crc32Reader::new( Ok(ZipFileReader::Deflated(Crc32Reader::new(
@ -426,7 +438,11 @@ fn get_reader<'a>(reader: &'a mut ZipFileReader<'_>) -> &'a mut dyn Read {
match *reader { match *reader {
ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"), ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"),
ZipFileReader::Stored(ref mut r) => r as &mut dyn Read, ZipFileReader::Stored(ref mut r) => r as &mut dyn Read,
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
ZipFileReader::Deflated(ref mut r) => r as &mut dyn Read, ZipFileReader::Deflated(ref mut r) => r as &mut dyn Read,
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
ZipFileReader::Bzip2(ref mut r) => r as &mut dyn Read, ZipFileReader::Bzip2(ref mut r) => r as &mut dyn Read,
@ -566,7 +582,11 @@ impl<'a> Drop for ZipFile<'a> {
let mut reader = match innerreader { let mut reader = match innerreader {
ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"), ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"),
ZipFileReader::Stored(crcreader) => crcreader.into_inner(), ZipFileReader::Stored(crcreader) => crcreader.into_inner(),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
ZipFileReader::Deflated(crcreader) => crcreader.into_inner().into_inner(), ZipFileReader::Deflated(crcreader) => crcreader.into_inner().into_inner(),
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
ZipFileReader::Bzip2(crcreader) => crcreader.into_inner().into_inner(), ZipFileReader::Bzip2(crcreader) => crcreader.into_inner().into_inner(),

View file

@ -11,7 +11,11 @@ use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::mem; use std::mem;
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
use flate2::write::DeflateEncoder; use flate2::write::DeflateEncoder;
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
@ -20,7 +24,11 @@ use bzip2::write::BzEncoder;
enum GenericZipWriter<W: Write + io::Seek> { enum GenericZipWriter<W: Write + io::Seek> {
Closed, Closed,
Storer(W), Storer(W),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
Deflater(DeflateEncoder<W>), Deflater(DeflateEncoder<W>),
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
Bzip2(BzEncoder<W>), Bzip2(BzEncoder<W>),
@ -77,9 +85,17 @@ impl FileOptions {
/// Construct a new FileOptions object /// Construct a new FileOptions object
pub fn default() -> FileOptions { pub fn default() -> FileOptions {
FileOptions { FileOptions {
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
compression_method: CompressionMethod::Deflated, compression_method: CompressionMethod::Deflated,
#[cfg(not(feature = "deflate"))] #[cfg(not(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
)))]
compression_method: CompressionMethod::Stored, compression_method: CompressionMethod::Stored,
#[cfg(feature = "time")] #[cfg(feature = "time")]
last_modified_time: DateTime::from_time(time::now()).unwrap_or_default(), last_modified_time: DateTime::from_time(time::now()).unwrap_or_default(),
@ -384,7 +400,11 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
let bare = match mem::replace(self, GenericZipWriter::Closed) { let bare = match mem::replace(self, GenericZipWriter::Closed) {
GenericZipWriter::Storer(w) => w, GenericZipWriter::Storer(w) => w,
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
GenericZipWriter::Deflater(w) => w.finish()?, GenericZipWriter::Deflater(w) => w.finish()?,
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(w) => w.finish()?, GenericZipWriter::Bzip2(w) => w.finish()?,
@ -401,7 +421,11 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
#[allow(deprecated)] #[allow(deprecated)]
match compression { match compression {
CompressionMethod::Stored => GenericZipWriter::Storer(bare), CompressionMethod::Stored => GenericZipWriter::Storer(bare),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
CompressionMethod::Deflated => GenericZipWriter::Deflater(DeflateEncoder::new( CompressionMethod::Deflated => GenericZipWriter::Deflater(DeflateEncoder::new(
bare, bare,
flate2::Compression::default(), flate2::Compression::default(),
@ -422,7 +446,11 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
fn ref_mut(&mut self) -> Option<&mut dyn Write> { fn ref_mut(&mut self) -> Option<&mut dyn Write> {
match *self { match *self {
GenericZipWriter::Storer(ref mut w) => Some(w as &mut dyn Write), GenericZipWriter::Storer(ref mut w) => Some(w as &mut dyn Write),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
GenericZipWriter::Deflater(ref mut w) => Some(w as &mut dyn Write), GenericZipWriter::Deflater(ref mut w) => Some(w as &mut dyn Write),
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut dyn Write), GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut dyn Write),
@ -447,7 +475,11 @@ impl<W: Write + io::Seek> GenericZipWriter<W> {
fn current_compression(&self) -> Option<CompressionMethod> { fn current_compression(&self) -> Option<CompressionMethod> {
match *self { match *self {
GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored), GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored),
#[cfg(feature = "deflate")] #[cfg(any(
feature = "deflate",
feature = "deflate-miniz",
feature = "deflate-zlib"
))]
GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated), GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated),
#[cfg(feature = "bzip2")] #[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2), GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2),