Switch to flate2

This commit is contained in:
jonpas 2019-06-14 15:25:00 +02:00
parent 6f9150d3dc
commit b171b915f8
6 changed files with 35 additions and 29 deletions

View file

@ -12,10 +12,10 @@ Library to support the reading and writing of zip files.
"""
[dependencies]
flate2 = { version = "1.0", default-features = false, optional = true }
time = { version = "0.1", optional = true }
podio = "0.1"
bzip2 = { version = "0.3", optional = true }
libflate = { version = ">=0.1.21", optional = true }
crc32fast = "1.0"
[dev-dependencies]
@ -24,7 +24,9 @@ rand = "0.4"
walkdir = "1.0"
[features]
deflate = ["libflate"]
deflate = ["flate2", "flate2/default"]
deflate-zlib = ["flate2", "flate2/zlib"]
deflate-rust = ["flate2", "flate2/rust_backend"]
default = ["bzip2", "deflate", "time"]
[[bench]]

View file

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

View file

@ -9,7 +9,7 @@ pub enum CompressionMethod
/// The file is stored (no compression)
Stored,
/// Deflate in pure rust
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
Deflated,
/// File is compressed using BZIP2 algorithm
#[cfg(feature = "bzip2")]
@ -23,7 +23,7 @@ impl CompressionMethod {
pub fn from_u16(val: u16) -> CompressionMethod {
match val {
0 => CompressionMethod::Stored,
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
8 => CompressionMethod::Deflated,
#[cfg(feature = "bzip2")]
12 => CompressionMethod::Bzip2,
@ -35,7 +35,7 @@ impl CompressionMethod {
pub fn to_u16(self) -> u16 {
match self {
CompressionMethod::Stored => 0,
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
CompressionMethod::Deflated => 8,
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => 12,

View file

@ -5,8 +5,8 @@
#[cfg(feature = "bzip2")]
extern crate bzip2;
extern crate crc32fast;
#[cfg(feature = "deflate")]
extern crate libflate;
#[cfg(feature = "flate2")]
extern crate flate2;
extern crate podio;
#[cfg(feature = "time")]
extern crate time;

View file

@ -13,8 +13,10 @@ use podio::{ReadPodExt, LittleEndian};
use types::{ZipFileData, System, DateTime};
use cp437::FromCp437;
#[cfg(feature = "deflate")]
use libflate;
#[cfg(feature = "flate2")]
use flate2;
#[cfg(feature = "flate2")]
use flate2::read::DeflateDecoder;
#[cfg(feature = "bzip2")]
use bzip2::read::BzDecoder;
@ -63,8 +65,8 @@ pub struct ZipArchive<R: Read + io::Seek>
enum ZipFileReader<'a> {
NoReader,
Stored(Crc32Reader<io::Take<&'a mut Read>>),
#[cfg(feature = "deflate")]
Deflated(Crc32Reader<libflate::deflate::Decoder<io::Take<&'a mut Read>>>),
#[cfg(feature = "flate2")]
Deflated(Crc32Reader<flate2::read::DeflateDecoder<io::Take<&'a mut Read>>>),
#[cfg(feature = "bzip2")]
Bzip2(Crc32Reader<BzDecoder<io::Take<&'a mut Read>>>),
}
@ -94,10 +96,10 @@ fn make_reader<'a>(
reader,
crc32)))
},
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
CompressionMethod::Deflated =>
{
let deflate_reader = libflate::deflate::Decoder::new(reader);
let deflate_reader = DeflateDecoder::new(reader);
Ok(ZipFileReader::Deflated(Crc32Reader::new(
deflate_reader,
crc32)))
@ -416,7 +418,7 @@ fn get_reader<'a>(reader: &'a mut ZipFileReader) -> &'a mut Read {
match *reader {
ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"),
ZipFileReader::Stored(ref mut r) => r as &mut Read,
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
ZipFileReader::Deflated(ref mut r) => r as &mut Read,
#[cfg(feature = "bzip2")]
ZipFileReader::Bzip2(ref mut r) => r as &mut Read,
@ -528,7 +530,7 @@ impl<'a> Drop for ZipFile<'a> {
let mut reader = match innerreader {
ZipFileReader::NoReader => panic!("ZipFileReader was in an invalid state"),
ZipFileReader::Stored(crcreader) => crcreader.into_inner(),
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
ZipFileReader::Deflated(crcreader) => crcreader.into_inner().into_inner(),
#[cfg(feature = "bzip2")]
ZipFileReader::Bzip2(crcreader) => crcreader.into_inner().into_inner(),

View file

@ -13,8 +13,10 @@ use std::mem;
use time;
use podio::{WritePodExt, LittleEndian};
#[cfg(feature = "deflate")]
use libflate;
#[cfg(feature = "flate2")]
use flate2;
#[cfg(feature = "flate2")]
use flate2::write::DeflateEncoder;
#[cfg(feature = "bzip2")]
use bzip2;
@ -25,8 +27,8 @@ enum GenericZipWriter<W: Write + io::Seek>
{
Closed,
Storer(W),
#[cfg(feature = "deflate")]
Deflater(libflate::deflate::Encoder<W>),
#[cfg(feature = "flate2")]
Deflater(DeflateEncoder<W>),
#[cfg(feature = "bzip2")]
Bzip2(BzEncoder<W>),
}
@ -83,8 +85,8 @@ impl FileOptions {
/// Construct a new FileOptions object
pub fn default() -> FileOptions {
FileOptions {
#[cfg(feature = "deflate")] compression_method: CompressionMethod::Deflated,
#[cfg(not(feature = "deflate"))] compression_method: CompressionMethod::Stored,
#[cfg(feature = "flate2")] compression_method: CompressionMethod::Deflated,
#[cfg(not(feature = "flate2"))] compression_method: CompressionMethod::Stored,
#[cfg(feature = "time")] last_modified_time: DateTime::from_time(time::now()).unwrap_or(DateTime::default()),
#[cfg(not(feature = "time"))] last_modified_time: DateTime::default(),
permissions: None,
@ -367,8 +369,8 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
let bare = match mem::replace(self, GenericZipWriter::Closed)
{
GenericZipWriter::Storer(w) => w,
#[cfg(feature = "deflate")]
GenericZipWriter::Deflater(w) => w.finish().into_result()?,
#[cfg(feature = "flate2")]
GenericZipWriter::Deflater(w) => w.finish()?,
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(w) => w.finish()?,
GenericZipWriter::Closed => Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed"))?,
@ -377,8 +379,8 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
*self = match compression
{
CompressionMethod::Stored => GenericZipWriter::Storer(bare),
#[cfg(feature = "deflate")]
CompressionMethod::Deflated => GenericZipWriter::Deflater(libflate::deflate::Encoder::new(bare)),
#[cfg(feature = "flate2")]
CompressionMethod::Deflated => GenericZipWriter::Deflater(DeflateEncoder::new(bare, flate2::Compression::default())),
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => GenericZipWriter::Bzip2(BzEncoder::new(bare, bzip2::Compression::Default)),
CompressionMethod::Unsupported(..) => return Err(ZipError::UnsupportedArchive("Unsupported compression")),
@ -390,7 +392,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
fn ref_mut(&mut self) -> Option<&mut Write> {
match *self {
GenericZipWriter::Storer(ref mut w) => Some(w as &mut Write),
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
GenericZipWriter::Deflater(ref mut w) => Some(w as &mut Write),
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut Write),
@ -419,7 +421,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
fn current_compression(&self) -> Option<CompressionMethod> {
match *self {
GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored),
#[cfg(feature = "deflate")]
#[cfg(feature = "flate2")]
GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated),
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2),