Split compression from types and pub use ZipFile

This commit is contained in:
Mathijs van de Nes 2014-09-15 21:33:58 +02:00
parent 8921d3b5c2
commit 0cd915f185
7 changed files with 59 additions and 56 deletions

View file

@ -18,10 +18,10 @@ fn doit(filename: &str) -> std::io::IoResult<()>
let mut zip = zip::ZipWriter::new(file);
try!(zip.start_file("test/☃.txt", zip::types::Stored));
try!(zip.start_file("test/☃.txt", zip::compression::Stored));
try!(zip.write(b"Hello, World!\n"));
try!(zip.start_file("test/lorem_ipsum.txt", zip::types::Deflated));
try!(zip.start_file("test/lorem_ipsum.txt", zip::compression::Deflated));
try!(zip.write(LOREM_IPSUM));
zip.finalize()

41
src/compression.rs Normal file
View file

@ -0,0 +1,41 @@
//! Possible ZIP compression methods.
/// Compression methods for the contents of a ZIP file.
#[deriving(FromPrimitive, Clone)]
pub enum CompressionMethod
{
/// The file is stored (no compression)
Stored = 0,
/// The file is Shrunk
Shrunk = 1,
/// The file is Reduced with compression factor 1
Reduced1 = 2,
/// The file is Reduced with compression factor 2
Reduced2 = 3,
/// The file is Reduced with compression factor 3
Reduced3 = 4,
/// The file is Reduced with compression factor 4
Reduced4 = 5,
/// The file is Imploded
Imploded = 6,
/// The file is Deflated
Deflated = 8,
/// Enhanced Deflating using Deflate64(tm)
Deflate64 = 9,
/// PKWARE Data Compression Library Imploding (old IBM TERSE)
PkwareImploding = 10,
/// File is compressed using BZIP2 algorithm
Bzip2 = 12,
/// LZMA (EFS)
LZMA = 14,
/// File is compressed using IBM TERSE (new)
IBMTerse = 18,
/// IBM LZ77 z Architecture (PFS)
LZ77 = 19,
/// WavPack compressed data
WavPack = 97,
/// PPMd version I, Rev 1
PPMdI1 = 98,
/// Unknown (invalid) compression
Unknown = 100000,
}

View file

@ -10,11 +10,13 @@ extern crate flate2;
pub use reader::ZipReader;
pub use writer::ZipWriter;
pub use types::ZipFile;
mod util;
mod spec;
mod crc32;
mod reader;
pub mod types;
mod types;
pub mod compression;
mod writer;
mod cp437;

View file

@ -1,7 +1,7 @@
use spec;
use crc32::Crc32Reader;
use types::ZipFile;
use types;
use compression;
use std::io;
use std::io::{IoResult, IoError};
use std::cell::RefCell;
@ -107,7 +107,7 @@ impl<T: Reader+Seek> ZipReader<T>
let reader = match file.compression_method
{
types::Stored =>
compression::Stored =>
{
box
Crc32Reader::new(
@ -115,7 +115,7 @@ impl<T: Reader+Seek> ZipReader<T>
file.crc32)
as Box<Reader>
},
types::Deflated =>
compression::Deflated =>
{
let deflate_reader = limit_reader.deflate_decode();
box

View file

@ -1,7 +1,7 @@
use std::io;
use std::io::{IoResult, IoError};
use std::iter::range_step_inclusive;
use types;
use compression;
use types::ZipFile;
use util;
@ -78,7 +78,7 @@ pub fn central_header_to_zip_file<R: Reader+Seek>(reader: &mut R) -> IoResult<Zi
let mut result = ZipFile
{
encrypted: encrypted,
compression_method: FromPrimitive::from_u16(compression_method).unwrap_or(types::Unknown),
compression_method: FromPrimitive::from_u16(compression_method).unwrap_or(compression::Unknown),
last_modified_time: util::msdos_datetime_to_tm(last_mod_time, last_mod_date),
crc32: crc32,
compressed_size: compressed_size as u64,

View file

@ -2,53 +2,13 @@
use time;
/// Compression methods for the contents of a ZIP file.
#[deriving(FromPrimitive, Clone)]
pub enum CompressionMethod
{
/// The file is stored (no compression)
Stored = 0,
/// The file is Shrunk
Shrunk = 1,
/// The file is Reduced with compression factor 1
Reduced1 = 2,
/// The file is Reduced with compression factor 2
Reduced2 = 3,
/// The file is Reduced with compression factor 3
Reduced3 = 4,
/// The file is Reduced with compression factor 4
Reduced4 = 5,
/// The file is Imploded
Imploded = 6,
/// The file is Deflated
Deflated = 8,
/// Enhanced Deflating using Deflate64(tm)
Deflate64 = 9,
/// PKWARE Data Compression Library Imploding (old IBM TERSE)
PkwareImploding = 10,
/// File is compressed using BZIP2 algorithm
Bzip2 = 12,
/// LZMA (EFS)
LZMA = 14,
/// File is compressed using IBM TERSE (new)
IBMTerse = 18,
/// IBM LZ77 z Architecture (PFS)
LZ77 = 19,
/// WavPack compressed data
WavPack = 97,
/// PPMd version I, Rev 1
PPMdI1 = 98,
/// Unknown (invalid) compression
Unknown = 100000,
}
/// Structure representing a ZIP file.
pub struct ZipFile
{
/// True if the file is encrypted.
pub encrypted: bool,
/// Compression method used to store the file
pub compression_method: CompressionMethod,
pub compression_method: ::compression::CompressionMethod,
/// Last modified time. This will only have a 2 second precision.
pub last_modified_time: time::Tm,
/// CRC32 checksum

View file

@ -1,4 +1,4 @@
use types;
use compression;
use types::ZipFile;
use spec;
use crc32;
@ -28,7 +28,7 @@ enum GenericZipWriter<W>
/// let w = std::io::BufWriter::new(&mut buf);
/// let mut zip = zip::ZipWriter::new(w);
///
/// try!(zip.start_file("hello_world.txt", zip::types::Stored));
/// try!(zip.start_file("hello_world.txt", zip::compression::Stored));
/// try!(zip.write(b"Hello, World!"));
///
/// // Optionally finish the zip. (this is also done on drop)
@ -99,7 +99,7 @@ impl<W: Writer+Seek> ZipWriter<W>
}
/// Start a new file for with the requested compression method.
pub fn start_file(&mut self, name: &str, compression: types::CompressionMethod) -> IoResult<()>
pub fn start_file(&mut self, name: &str, compression: compression::CompressionMethod) -> IoResult<()>
{
try!(self.finish_file());
@ -139,7 +139,7 @@ impl<W: Writer+Seek> ZipWriter<W>
fn finish_file(&mut self) -> IoResult<()>
{
try!(self.inner.switch_to(types::Stored));
try!(self.inner.switch_to(compression::Stored));
let writer = self.inner.get_plain();
let file = match self.files.mut_last()
@ -209,7 +209,7 @@ impl<W: Writer+Seek> Drop for ZipWriter<W>
impl<W: Writer+Seek> GenericZipWriter<W>
{
fn switch_to(&mut self, compression: types::CompressionMethod) -> IoResult<()>
fn switch_to(&mut self, compression: compression::CompressionMethod) -> IoResult<()>
{
let bare = match mem::replace(self, Closed)
{
@ -220,8 +220,8 @@ impl<W: Writer+Seek> GenericZipWriter<W>
*self = match compression
{
types::Stored => Storer(bare),
types::Deflated => Deflater(bare.deflate_encode(flate2::Default)),
compression::Stored => Storer(bare),
compression::Deflated => Deflater(bare.deflate_encode(flate2::Default)),
_ => return Err(IoError { kind: io::OtherIoError, desc: "Unsupported compression requested", detail: None }),
};