pub struct Decoder { /* private fields */ }Expand description
A streaming gzip decoder.
Multi-member streams (RFC 1952 section 2.2) are decoded as one by
default, up to Limits::max_members; the output cap applies to all
members together. With more input possible after a member, decompress
returns NeedsInput; finish decides that the stream ended.
§Examples
use compress_lib::{deflate::Status, gzip};
let packed = gzip::compress(b"streamed");
let mut dec = gzip::Decoder::new();
let mut out = [0u8; 32];
let p = dec.finish(&packed, &mut out)?;
assert_eq!(p.status, Status::Done);
assert_eq!(&out[..p.produced], b"streamed");Implementations§
Source§impl Decoder
impl Decoder
Sourcepub fn new() -> Self
pub fn new() -> Self
A decoder with window bits 15 and the default limits, multi-member, trailing data refused, header fields not kept.
§Examples
assert!(!compress_lib::gzip::Decoder::new().is_done());Sourcepub fn with_limits(self, limits: Limits) -> Self
pub fn with_limits(self, limits: Limits) -> Self
Sets the limits: the output cap (across members), the ratio cap, the header field cap, and the member cap.
§Examples
use compress_lib::{Limits, gzip};
let dec = gzip::Decoder::new().with_limits(Limits::new().with_max_members(1));Sourcepub fn with_single_member(self) -> Self
pub fn with_single_member(self) -> Self
Decodes one member only; Done at its end, and what follows is left
to the caller.
§Examples
use compress_lib::{deflate::Status, gzip};
let mut two = gzip::compress(b"first");
let first_len = two.len();
two.extend_from_slice(&gzip::compress(b"second"));
let mut dec = gzip::Decoder::new().with_single_member();
let mut out = Vec::new();
let p = dec.decompress_vec(&two, &mut out)?;
assert_eq!((p.status, p.consumed), (Status::Done, first_len));Sourcepub fn with_trailing_data(self, allow: bool) -> Self
pub fn with_trailing_data(self, allow: bool) -> Self
Allows bytes that are not a gzip member after the last member (some
tools pad files with zeros); consumed stops before them.
§Examples
use compress_lib::gzip;
let mut padded = gzip::compress(b"data");
padded.extend_from_slice(&[0; 8]);
let mut dec = gzip::Decoder::new().with_trailing_data(true);
let mut out = Vec::new();
dec.finish_vec(&padded, &mut out)?;
assert_eq!(out, b"data");Sourcepub fn with_keep_header(self, keep: bool) -> Self
pub fn with_keep_header(self, keep: bool) -> Self
Keeps the header fields (name, comment, extra) for header
(bounded by Limits::max_header); off by default, so no memory is
spent on them.
§Examples
let dec = compress_lib::gzip::Decoder::new().with_keep_header(true);
assert!(dec.header().is_none());Sourcepub fn header(&self) -> Option<&GzipHeader>
pub fn header(&self) -> Option<&GzipHeader>
The first member’s header, once read, when headers are kept.
§Examples
use compress_lib::gzip;
let mut dec = gzip::Decoder::new().with_keep_header(true);
dec.finish_vec(&gzip::compress(b"x"), &mut Vec::new())?;
assert_eq!(dec.header().map(|h| h.os()), Some(255));Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
true once the stream ended.
§Examples
use compress_lib::gzip;
let mut dec = gzip::Decoder::new();
dec.finish_vec(&gzip::compress(b""), &mut Vec::new())?;
assert!(dec.is_done());Sourcepub fn members(&self) -> u32
pub fn members(&self) -> u32
Members completed so far.
§Examples
use compress_lib::gzip;
let mut two = gzip::compress(b"a");
two.extend_from_slice(&gzip::compress(b"b"));
let mut dec = gzip::Decoder::new();
dec.finish_vec(&two, &mut Vec::new())?;
assert_eq!(dec.members(), 2);Sourcepub fn total_in(&self) -> u64
pub fn total_in(&self) -> u64
Compressed bytes consumed.
§Examples
use compress_lib::gzip;
let packed = gzip::compress(b"x");
let mut dec = gzip::Decoder::new();
dec.finish_vec(&packed, &mut Vec::new())?;
assert_eq!(dec.total_in(), packed.len() as u64);Sourcepub fn total_out(&self) -> u64
pub fn total_out(&self) -> u64
Decompressed bytes produced, all members.
§Examples
use compress_lib::gzip;
let mut dec = gzip::Decoder::new();
dec.finish_vec(&gzip::compress(b"four"), &mut Vec::new())?;
assert_eq!(dec.total_out(), 4);Sourcepub fn memory_in_use(&self) -> usize
pub fn memory_in_use(&self) -> usize
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Starts over for a new stream, keeping allocations.
§Examples
let mut dec = compress_lib::gzip::Decoder::new();
dec.reset();Sourcepub fn decompress(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<Progress, Error>
pub fn decompress( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>
Decompresses as much of input into output as fits; more input may
follow (and after a member, perhaps another member).
§Errors
Decoding errors, the header and trailer checks, and the limits.
§Examples
use compress_lib::{deflate::Status, gzip};
let packed = gzip::compress(b"split");
let mut dec = gzip::Decoder::new();
let mut out = [0u8; 16];
let a = dec.decompress(&packed[..5], &mut out)?;
assert_eq!(a.status, Status::NeedsInput);
let b = dec.finish(&packed[5..], &mut out[a.produced..])?;
assert_eq!(b.status, Status::Done);Sourcepub fn finish(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<Progress, Error>
pub fn finish( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Progress, Error>
As decompress, with input the last of the
stream.
§Errors
As decompress, plus UnexpectedEnd.
§Examples
use compress_lib::{ErrorKind, gzip};
let packed = gzip::compress(b"data");
let err = gzip::Decoder::new().finish(&packed[..packed.len() - 1], &mut [0u8; 8]).unwrap_err();
assert_eq!(err.kind(), ErrorKind::UnexpectedEnd);Sourcepub fn decompress_vec(
&mut self,
input: &[u8],
out: &mut Vec<u8>,
) -> Result<Progress, Error>
pub fn decompress_vec( &mut self, input: &[u8], out: &mut Vec<u8>, ) -> Result<Progress, Error>
Decompresses input, appending to out.
§Errors
As decompress, plus AllocationFailed.
§Examples
use compress_lib::gzip;
let mut out = Vec::new();
gzip::Decoder::new().decompress_vec(&gzip::compress(b"vec"), &mut out)?;
assert_eq!(out, b"vec");Sourcepub fn finish_vec(
&mut self,
input: &[u8],
out: &mut Vec<u8>,
) -> Result<Progress, Error>
pub fn finish_vec( &mut self, input: &[u8], out: &mut Vec<u8>, ) -> Result<Progress, Error>
As decompress_vec, with input the last
of the stream.
§Errors
As decompress_vec, plus UnexpectedEnd.
§Examples
use compress_lib::gzip;
let mut out = Vec::new();
gzip::Decoder::new().finish_vec(&gzip::compress(b"vec"), &mut out)?;
assert_eq!(out, b"vec");