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

View file

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

View file

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

View file

@ -7,6 +7,7 @@ use result::{ZipResult, ZipError};
use std::io;
use std::cell::RefCell;
use flate2::FlateReader;
use bzip2::reader::BzDecompressor;
/// Wrapper for reading the contents of a ZIP file.
///
@ -112,6 +113,15 @@ impl<T: Reader+Seek> ZipReader<T>
file.crc32)
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"),
};
Ok(reader)

View file

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