Add bzip2 support

This commit is contained in:
Mathijs van de Nes 2014-11-21 22:56:08 +01:00
parent 933f08a336
commit ee6e830203
5 changed files with 20 additions and 1 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "zip" name = "zip"
version = "0.0.1" version = "0.0.2"
authors = ["Mathijs van de Nes <git@mathijs.vd-nes.nl>"] authors = ["Mathijs van de Nes <git@mathijs.vd-nes.nl>"]
license = "MIT" license = "MIT"
repository = "https://github.com/mvdnes/zip-rs.git" repository = "https://github.com/mvdnes/zip-rs.git"
@ -11,6 +11,7 @@ Library to support the reading and writing of zip files.
[dependencies] [dependencies]
flate2 = "*" flate2 = "*"
bzip2 = "*"
time = "*" time = "*"
[[bin]] [[bin]]

View file

@ -11,6 +11,7 @@ Supported compression formats:
* stored (i.e. none) * stored (i.e. none)
* deflate * deflate
* bzip2
Currently unsupported zip extensions: Currently unsupported zip extensions:

View file

@ -7,6 +7,7 @@
#[phase(plugin, link)] extern crate log; #[phase(plugin, link)] extern crate log;
extern crate time; extern crate time;
extern crate flate2; extern crate flate2;
extern crate bzip2;
pub use reader::ZipReader; pub use reader::ZipReader;
pub use writer::ZipWriter; pub use writer::ZipWriter;

View file

@ -7,6 +7,7 @@ use result::{ZipResult, ZipError};
use std::io; use std::io;
use std::cell::RefCell; use std::cell::RefCell;
use flate2::FlateReader; use flate2::FlateReader;
use bzip2::reader::BzDecompressor;
/// Wrapper for reading the contents of a ZIP file. /// Wrapper for reading the contents of a ZIP file.
/// ///
@ -112,6 +113,15 @@ impl<T: Reader+Seek> ZipReader<T>
file.crc32) file.crc32)
as Box<Reader> as Box<Reader>
}, },
CompressionMethod::Bzip2 =>
{
let bzip2_reader = BzDecompressor::new(limit_reader);
box
Crc32Reader::new(
bzip2_reader,
file.crc32)
as Box<Reader>
},
_ => return unsupported_zip_error("Compression method not supported"), _ => return unsupported_zip_error("Compression method not supported"),
}; };
Ok(reader) Ok(reader)

View file

@ -11,12 +11,15 @@ use time;
use flate2; use flate2;
use flate2::FlateWriter; use flate2::FlateWriter;
use flate2::writer::DeflateEncoder; use flate2::writer::DeflateEncoder;
use bzip2;
use bzip2::writer::BzCompressor;
enum GenericZipWriter<W> enum GenericZipWriter<W>
{ {
Closed, Closed,
Storer(W), Storer(W),
Deflater(DeflateEncoder<W>), Deflater(DeflateEncoder<W>),
Bzip2(BzCompressor<W>),
} }
/// Generator for ZIP files. /// Generator for ZIP files.
@ -65,6 +68,7 @@ impl<W: Writer+Seek> Writer for ZipWriter<W>
{ {
GenericZipWriter::Storer(ref mut w) => w.write(buf), GenericZipWriter::Storer(ref mut w) => w.write(buf),
GenericZipWriter::Deflater(ref mut w) => w.write(buf), GenericZipWriter::Deflater(ref mut w) => w.write(buf),
GenericZipWriter::Bzip2(ref mut w) => w.write(buf),
GenericZipWriter::Closed => Err(io::standard_error(io::Closed)), GenericZipWriter::Closed => Err(io::standard_error(io::Closed)),
} }
} }
@ -219,6 +223,7 @@ impl<W: Writer+Seek> GenericZipWriter<W>
{ {
GenericZipWriter::Storer(w) => w, GenericZipWriter::Storer(w) => w,
GenericZipWriter::Deflater(w) => try!(w.finish()), GenericZipWriter::Deflater(w) => try!(w.finish()),
GenericZipWriter::Bzip2(w) => try!(w.unwrap()),
GenericZipWriter::Closed => try!(Err(io::standard_error(io::Closed))), GenericZipWriter::Closed => try!(Err(io::standard_error(io::Closed))),
}; };
@ -226,6 +231,7 @@ impl<W: Writer+Seek> GenericZipWriter<W>
{ {
CompressionMethod::Stored => GenericZipWriter::Storer(bare), CompressionMethod::Stored => GenericZipWriter::Storer(bare),
CompressionMethod::Deflated => GenericZipWriter::Deflater(bare.deflate_encode(flate2::Default)), CompressionMethod::Deflated => GenericZipWriter::Deflater(bare.deflate_encode(flate2::Default)),
CompressionMethod::Bzip2 => GenericZipWriter::Bzip2(BzCompressor::new(bare, bzip2::CompressionLevel::Default)),
_ => return Err(ZipError::UnsupportedZipFile("Unsupported compression")), _ => return Err(ZipError::UnsupportedZipFile("Unsupported compression")),
}; };