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]
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 = []

View file

@ -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<CompressionMethod> {
vec![CompressionMethod::Stored, CompressionMethod::Deflated]
}
#[cfg(feature = "bzip2")]
fn methods() -> Vec<CompressionMethod> {
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);
}
}
}

View file

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

View file

@ -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<R: Read + io::Seek>
enum ZipFileReader<'a> {
Stored(Crc32Reader<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>>>),
}
@ -147,6 +150,7 @@ impl<R: Read+io::Seek> ZipArchive<R>
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,
}
}

View file

@ -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<W: Write + io::Seek>
{
Closed,
Storer(W),
Deflater(DeflateEncoder<W>),
#[cfg(feature = "bzip2")]
Bzip2(BzCompressor<W>),
}
@ -241,6 +245,7 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
{
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<W: Write+io::Seek> GenericZipWriter<W>
{
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<W: Write+io::Seek> GenericZipWriter<W>
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<W: Write+io::Seek> GenericZipWriter<W>
match *self {
GenericZipWriter::Storer(..) => Some(CompressionMethod::Stored),
GenericZipWriter::Deflater(..) => Some(CompressionMethod::Deflated),
#[cfg(feature = "bzip2")]
GenericZipWriter::Bzip2(..) => Some(CompressionMethod::Bzip2),
GenericZipWriter::Closed => None,
}