Made bzip2 compression method optional

This commit is contained in:
Alexander Bulaev 2015-10-05 20:01:41 +03:00
parent 8afc0a7a7c
commit 2addfa8b81
5 changed files with 38 additions and 7 deletions

View file

@ -13,7 +13,11 @@ Library to support the reading and writing of zip files.
[dependencies] [dependencies]
flate2 = "0.2" flate2 = "0.2"
bzip2 = "0.2"
time = "0.1" time = "0.1"
podio = "0.1" podio = "0.1"
msdos_time = "0.1" msdos_time = "0.1"
bzip2 = { version = "0.2", optional = true }
[features]
default = ["bzip2"]
no-bzip2 = []

View file

@ -9,6 +9,7 @@ pub enum CompressionMethod
/// The file is Deflated /// The file is Deflated
Deflated, Deflated,
/// File is compressed using BZIP2 algorithm /// File is compressed using BZIP2 algorithm
#[cfg(feature = "bzip2")]
Bzip2, Bzip2,
/// Unsupported compression method /// Unsupported compression method
Unsupported(u16), Unsupported(u16),
@ -20,6 +21,7 @@ impl CompressionMethod {
match val { match val {
0 => CompressionMethod::Stored, 0 => CompressionMethod::Stored,
8 => CompressionMethod::Deflated, 8 => CompressionMethod::Deflated,
#[cfg(feature = "bzip2")]
12 => CompressionMethod::Bzip2, 12 => CompressionMethod::Bzip2,
v => CompressionMethod::Unsupported(v), v => CompressionMethod::Unsupported(v),
} }
@ -30,6 +32,7 @@ impl CompressionMethod {
match self { match self {
CompressionMethod::Stored => 0, CompressionMethod::Stored => 0,
CompressionMethod::Deflated => 8, CompressionMethod::Deflated => 8,
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => 12, CompressionMethod::Bzip2 => 12,
CompressionMethod::Unsupported(v) => v, CompressionMethod::Unsupported(v) => v,
} }
@ -50,6 +53,16 @@ mod test {
} }
} }
#[cfg(not(feature = "bzip2"))]
fn methods() -> Vec<CompressionMethod> {
vec![CompressionMethod::Stored, CompressionMethod::Deflated]
}
#[cfg(feature = "bzip2")]
fn methods() -> Vec<CompressionMethod> {
vec![CompressionMethod::Stored, CompressionMethod::Deflated, CompressionMethod::Bzip2]
}
#[test] #[test]
fn to_eq_from() { fn to_eq_from() {
fn check_match(method: CompressionMethod) { fn check_match(method: CompressionMethod) {
@ -59,8 +72,8 @@ mod test {
assert_eq!(to, back); assert_eq!(to, back);
} }
check_match(CompressionMethod::Stored); for method in methods() {
check_match(CompressionMethod::Deflated); check_match(method);
check_match(CompressionMethod::Bzip2); }
} }
} }

View file

@ -2,6 +2,7 @@
#![warn(missing_docs)] #![warn(missing_docs)]
#[cfg(feature = "bzip2")]
extern crate bzip2; extern crate bzip2;
extern crate flate2; extern crate flate2;
extern crate msdos_time; extern crate msdos_time;

View file

@ -9,12 +9,14 @@ use std::io::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
use flate2; use flate2;
use flate2::FlateReadExt; use flate2::FlateReadExt;
use bzip2::reader::BzDecompressor;
use podio::{ReadPodExt, LittleEndian}; use podio::{ReadPodExt, LittleEndian};
use types::ZipFileData; use types::ZipFileData;
use cp437::FromCp437; use cp437::FromCp437;
use msdos_time::{TmMsDosExt, MsDosDateTime}; use msdos_time::{TmMsDosExt, MsDosDateTime};
#[cfg(feature = "bzip2")]
use bzip2::reader::BzDecompressor;
/// Wrapper for reading the contents of a ZIP file. /// Wrapper for reading the contents of a ZIP file.
/// ///
/// ``` /// ```
@ -51,6 +53,7 @@ pub struct ZipArchive<R: Read + io::Seek>
enum ZipFileReader<'a> { enum ZipFileReader<'a> {
Stored(Crc32Reader<io::Take<&'a mut Read>>), Stored(Crc32Reader<io::Take<&'a mut Read>>),
Deflated(Crc32Reader<flate2::read::DeflateDecoder<io::Take<&'a mut Read>>>), Deflated(Crc32Reader<flate2::read::DeflateDecoder<io::Take<&'a mut Read>>>),
#[cfg(feature = "bzip2")]
Bzip2(Crc32Reader<BzDecompressor<io::Take<&'a mut Read>>>), Bzip2(Crc32Reader<BzDecompressor<io::Take<&'a mut Read>>>),
} }
@ -147,6 +150,7 @@ impl<R: Read+io::Seek> ZipArchive<R>
deflate_reader, deflate_reader,
data.crc32)) data.crc32))
}, },
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => CompressionMethod::Bzip2 =>
{ {
let bzip2_reader = BzDecompressor::new(limit_reader); let bzip2_reader = BzDecompressor::new(limit_reader);
@ -272,6 +276,7 @@ impl<'a> ZipFile<'a> {
match self.reader { match self.reader {
ZipFileReader::Stored(ref mut r) => r as &mut Read, ZipFileReader::Stored(ref mut r) => r as &mut Read,
ZipFileReader::Deflated(ref mut r) => r as &mut Read, ZipFileReader::Deflated(ref mut r) => r as &mut Read,
#[cfg(feature = "bzip2")]
ZipFileReader::Bzip2(ref mut r) => r as &mut Read, ZipFileReader::Bzip2(ref mut r) => r as &mut Read,
} }
} }

View file

@ -14,16 +14,20 @@ use time;
use flate2; use flate2;
use flate2::FlateWriteExt; use flate2::FlateWriteExt;
use flate2::write::DeflateEncoder; use flate2::write::DeflateEncoder;
use bzip2;
use bzip2::writer::BzCompressor;
use podio::{WritePodExt, LittleEndian}; use podio::{WritePodExt, LittleEndian};
use msdos_time::TmMsDosExt; use msdos_time::TmMsDosExt;
#[cfg(feature = "bzip2")]
use bzip2;
#[cfg(feature = "bzip2")]
use bzip2::writer::BzCompressor;
enum GenericZipWriter<W: Write + io::Seek> enum GenericZipWriter<W: Write + io::Seek>
{ {
Closed, Closed,
Storer(W), Storer(W),
Deflater(DeflateEncoder<W>), Deflater(DeflateEncoder<W>),
#[cfg(feature = "bzip2")]
Bzip2(BzCompressor<W>), Bzip2(BzCompressor<W>),
} }
@ -241,6 +245,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
{ {
GenericZipWriter::Storer(w) => w, GenericZipWriter::Storer(w) => w,
GenericZipWriter::Deflater(w) => try!(w.finish()), GenericZipWriter::Deflater(w) => try!(w.finish()),
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(w) => match w.into_inner() { Ok(r) => r, Err((_, err)) => try!(Err(err)) }, GenericZipWriter::Bzip2(w) => match w.into_inner() { Ok(r) => r, Err((_, err)) => try!(Err(err)) },
GenericZipWriter::Closed => try!(Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed"))), GenericZipWriter::Closed => try!(Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed"))),
}; };
@ -249,6 +254,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
{ {
CompressionMethod::Stored => GenericZipWriter::Storer(bare), CompressionMethod::Stored => GenericZipWriter::Storer(bare),
CompressionMethod::Deflated => GenericZipWriter::Deflater(bare.deflate_encode(flate2::Compression::Default)), CompressionMethod::Deflated => GenericZipWriter::Deflater(bare.deflate_encode(flate2::Compression::Default)),
#[cfg(feature = "bzip2")]
CompressionMethod::Bzip2 => GenericZipWriter::Bzip2(BzCompressor::new(bare, bzip2::Compress::Default)), CompressionMethod::Bzip2 => GenericZipWriter::Bzip2(BzCompressor::new(bare, bzip2::Compress::Default)),
CompressionMethod::Unsupported(..) => return Err(ZipError::UnsupportedArchive("Unsupported compression")), CompressionMethod::Unsupported(..) => return Err(ZipError::UnsupportedArchive("Unsupported compression")),
}; };
@ -260,6 +266,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
match *self { match *self {
GenericZipWriter::Storer(ref mut w) => Some(w as &mut Write), GenericZipWriter::Storer(ref mut w) => Some(w as &mut Write),
GenericZipWriter::Deflater(ref mut w) => Some(w as &mut Write), GenericZipWriter::Deflater(ref mut w) => Some(w as &mut Write),
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut Write), GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut Write),
GenericZipWriter::Closed => None, GenericZipWriter::Closed => None,
} }
@ -287,6 +294,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
match *self { match *self {
GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored), GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored),
GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated), GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated),
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2), GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2),
GenericZipWriter::Closed => None, GenericZipWriter::Closed => None,
} }