diff --git a/Cargo.toml b/Cargo.toml index 21cea0db..a0e3d428 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,11 @@ Library to support the reading and writing of zip files. [dependencies] flate2 = "0.2" -bzip2 = "0.2" time = "0.1" podio = "0.1" msdos_time = "0.1" +bzip2 = { version = "0.2", optional = true } + +[features] +default = ["bzip2"] +no-bzip2 = [] diff --git a/src/compression.rs b/src/compression.rs index 5abd5704..295de6d4 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -9,6 +9,7 @@ pub enum CompressionMethod /// The file is Deflated Deflated, /// File is compressed using BZIP2 algorithm + #[cfg(feature = "bzip2")] Bzip2, /// Unsupported compression method Unsupported(u16), @@ -20,6 +21,7 @@ impl CompressionMethod { match val { 0 => CompressionMethod::Stored, 8 => CompressionMethod::Deflated, + #[cfg(feature = "bzip2")] 12 => CompressionMethod::Bzip2, v => CompressionMethod::Unsupported(v), } @@ -30,6 +32,7 @@ impl CompressionMethod { match self { CompressionMethod::Stored => 0, CompressionMethod::Deflated => 8, + #[cfg(feature = "bzip2")] CompressionMethod::Bzip2 => 12, CompressionMethod::Unsupported(v) => v, } @@ -50,6 +53,16 @@ mod test { } } + #[cfg(not(feature = "bzip2"))] + fn methods() -> Vec { + vec![CompressionMethod::Stored, CompressionMethod::Deflated] + } + + #[cfg(feature = "bzip2")] + fn methods() -> Vec { + vec![CompressionMethod::Stored, CompressionMethod::Deflated, CompressionMethod::Bzip2] + } + #[test] fn to_eq_from() { fn check_match(method: CompressionMethod) { @@ -59,8 +72,8 @@ mod test { assert_eq!(to, back); } - check_match(CompressionMethod::Stored); - check_match(CompressionMethod::Deflated); - check_match(CompressionMethod::Bzip2); + for method in methods() { + check_match(method); + } } } diff --git a/src/lib.rs b/src/lib.rs index 09f3f7ab..93e991c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ #![warn(missing_docs)] +#[cfg(feature = "bzip2")] extern crate bzip2; extern crate flate2; extern crate msdos_time; diff --git a/src/read.rs b/src/read.rs index 0bd88e43..5b6882cf 100644 --- a/src/read.rs +++ b/src/read.rs @@ -9,12 +9,14 @@ use std::io::prelude::*; use std::collections::HashMap; use flate2; use flate2::FlateReadExt; -use bzip2::reader::BzDecompressor; use podio::{ReadPodExt, LittleEndian}; use types::ZipFileData; use cp437::FromCp437; use msdos_time::{TmMsDosExt, MsDosDateTime}; +#[cfg(feature = "bzip2")] +use bzip2::reader::BzDecompressor; + /// Wrapper for reading the contents of a ZIP file. /// /// ``` @@ -51,6 +53,7 @@ pub struct ZipArchive enum ZipFileReader<'a> { Stored(Crc32Reader>), Deflated(Crc32Reader>>), + #[cfg(feature = "bzip2")] Bzip2(Crc32Reader>>), } @@ -147,6 +150,7 @@ impl ZipArchive deflate_reader, data.crc32)) }, + #[cfg(feature = "bzip2")] CompressionMethod::Bzip2 => { let bzip2_reader = BzDecompressor::new(limit_reader); @@ -272,6 +276,7 @@ impl<'a> ZipFile<'a> { match self.reader { ZipFileReader::Stored(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, } } diff --git a/src/write.rs b/src/write.rs index e644850a..8f407d25 100644 --- a/src/write.rs +++ b/src/write.rs @@ -14,16 +14,20 @@ use time; use flate2; use flate2::FlateWriteExt; use flate2::write::DeflateEncoder; -use bzip2; -use bzip2::writer::BzCompressor; use podio::{WritePodExt, LittleEndian}; use msdos_time::TmMsDosExt; +#[cfg(feature = "bzip2")] +use bzip2; +#[cfg(feature = "bzip2")] +use bzip2::writer::BzCompressor; + enum GenericZipWriter { Closed, Storer(W), Deflater(DeflateEncoder), + #[cfg(feature = "bzip2")] Bzip2(BzCompressor), } @@ -241,6 +245,7 @@ impl GenericZipWriter { GenericZipWriter::Storer(w) => w, 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::Closed => try!(Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed"))), }; @@ -249,6 +254,7 @@ impl GenericZipWriter { CompressionMethod::Stored => GenericZipWriter::Storer(bare), CompressionMethod::Deflated => GenericZipWriter::Deflater(bare.deflate_encode(flate2::Compression::Default)), + #[cfg(feature = "bzip2")] CompressionMethod::Bzip2 => GenericZipWriter::Bzip2(BzCompressor::new(bare, bzip2::Compress::Default)), CompressionMethod::Unsupported(..) => return Err(ZipError::UnsupportedArchive("Unsupported compression")), }; @@ -260,6 +266,7 @@ impl GenericZipWriter match *self { GenericZipWriter::Storer(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::Closed => None, } @@ -287,6 +294,7 @@ impl GenericZipWriter match *self { GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored), GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated), + #[cfg(feature = "bzip2")] GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2), GenericZipWriter::Closed => None, }