1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#![doc(html_root_url = "http://alexcrichton.com/flate2-rs")]
#![deny(missing_docs)]
#![allow(trivial_numeric_casts)]
#![cfg_attr(test, deny(warnings))]
extern crate libc;
extern crate miniz_sys as ffi;
#[cfg(test)] extern crate rand;
use std::io::prelude::*;
use std::io;
pub use gz::Builder as GzBuilder;
pub use gz::Header as GzHeader;
mod crc;
mod deflate;
mod gz;
mod raw;
mod zlib;
mod stream;
pub mod read {
pub use deflate::EncoderReader as DeflateEncoder;
pub use deflate::DecoderReader as DeflateDecoder;
pub use zlib::EncoderReader as ZlibEncoder;
pub use zlib::DecoderReader as ZlibDecoder;
pub use gz::EncoderReader as GzEncoder;
pub use gz::DecoderReader as GzDecoder;
}
pub mod write {
pub use deflate::EncoderWriter as DeflateEncoder;
pub use deflate::DecoderWriter as DeflateDecoder;
pub use zlib::EncoderWriter as ZlibEncoder;
pub use zlib::DecoderWriter as ZlibDecoder;
pub use gz::EncoderWriter as GzEncoder;
}
#[derive(Copy, Clone)]
pub enum Compression {
None = 0,
Fast = 1,
Best = 9,
Default = 6,
}
pub trait FlateReadExt: Read + Sized {
fn gz_encode(self, lvl: Compression) -> read::GzEncoder<Self> {
read::GzEncoder::new(self, lvl)
}
fn gz_decode(self) -> io::Result<read::GzDecoder<Self>> {
read::GzDecoder::new(self)
}
fn zlib_encode(self, lvl: Compression) -> read::ZlibEncoder<Self> {
read::ZlibEncoder::new(self, lvl)
}
fn zlib_decode(self) -> read::ZlibDecoder<Self> {
read::ZlibDecoder::new(self)
}
fn deflate_encode(self, lvl: Compression) -> read::DeflateEncoder<Self> {
read::DeflateEncoder::new(self, lvl)
}
fn deflate_decode(self) -> read::DeflateDecoder<Self> {
read::DeflateDecoder::new(self)
}
}
pub trait FlateWriteExt: Write + Sized {
fn gz_encode(self, lvl: Compression) -> write::GzEncoder<Self> {
write::GzEncoder::new(self, lvl)
}
fn zlib_encode(self, lvl: Compression) -> write::ZlibEncoder<Self> {
write::ZlibEncoder::new(self, lvl)
}
fn zlib_decode(self) -> write::ZlibDecoder<Self> {
write::ZlibDecoder::new(self)
}
fn deflate_encode(self, lvl: Compression) -> write::DeflateEncoder<Self> {
write::DeflateEncoder::new(self, lvl)
}
fn deflate_decode(self) -> write::DeflateDecoder<Self> {
write::DeflateDecoder::new(self)
}
}
impl<T: Read> FlateReadExt for T {}
impl<T: Write> FlateWriteExt for T {}
#[cfg(test)]
mod test {
use std::io::prelude::*;
use {FlateReadExt, Compression};
#[test]
fn crazy() {
let rdr = &mut b"foobar";
let mut res = Vec::new();
rdr.gz_encode(Compression::Default)
.deflate_encode(Compression::Default)
.zlib_encode(Compression::Default)
.zlib_decode()
.deflate_decode()
.gz_decode().unwrap()
.read_to_end(&mut res).unwrap();
assert_eq!(res, b"foobar");
}
}