Struct ZlibDecoder
pub struct ZlibDecoder<W: Write> { /* private fields */ }
A ZLIB decoder, or decompressor.
This structure implements a Write and will emit a stream of decompressed
data when fed a stream of compressed data.
After decoding a single member of the ZLIB data this writer will return the number of bytes up to the end of the ZLIB member and subsequent writes will return Ok(0) allowing the caller to handle any data following the ZLIB member.
Examples
use std::io::prelude::*;
use std::io;
# use flate2::Compression;
# use flate2::write::ZlibEncoder;
use flate2::write::ZlibDecoder;
# fn main() {
# let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
# e.write_all(b"Hello World").unwrap();
# let bytes = e.finish().unwrap();
# println!("{}", decode_reader(bytes).unwrap());
# }
#
// Uncompresses a Zlib Encoded vector of bytes and returns a string or error
// Here Vec<u8> implements Write
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
let mut writer = Vec::new();
let mut z = ZlibDecoder::new(writer);
z.write_all(&bytes[..])?;
writer = z.finish()?;
let return_string = String::from_utf8(writer).expect("String parsing error");
Ok(return_string)
}
Implementations
impl<W: Write> ZlibDecoder<W>
fn new(w: W) -> ZlibDecoder<W>Creates a new decoder which will write uncompressed data to the stream.
When this decoder is dropped or unwrapped the final pieces of data will be flushed.
fn new_with_decompress(w: W, decompression: Decompress) -> ZlibDecoder<W>Creates a new decoder which will write uncompressed data to the stream
wusing the givendecompressionsettings.When this decoder is dropped or unwrapped the final pieces of data will be flushed.
fn get_ref(&self) -> &WAcquires a reference to the underlying writer.
fn get_mut(&mut self) -> &mut WAcquires a mutable reference to the underlying writer.
The underlying writer may be mutated or replaced as long as this preserves the bytes and ordering of the logical output stream. Concatenate output from each writer to reconstruct the complete stream.
Replacing the writer does not require
flush. Call it first to write all decompressed output currently available to the current writer. This is useful before applyingstd::mem::taketoget_mutwhen forwarding output incrementally.To start a new stream, use
reset; replacing the writer does not reset this decoder.fn reset(&mut self, w: W) -> Result<W>Resets the state of this decoder entirely, swapping out the output stream for another.
This will reset the internal state of this decoder and replace the output stream with the one provided, returning the previous output stream. Future data written to this decoder will be decompressed into the output stream
w.Errors
This function will perform I/O to complete this stream, and any I/O errors which occur will be returned from this function.
fn try_finish(&mut self) -> Result<()>Attempt to finish this output stream, writing out final chunks of data.
Note that this function can only be used once data has finished being written to the output stream. After this function is called then further calls to
writemay result in a panic.Panics
Attempts to write data to this stream may result in a panic after this function is called.
Errors
This function will perform I/O to complete this stream, and any I/O errors which occur will be returned from this function.
fn finish(self) -> Result<W>Consumes this encoder, flushing the output stream.
This will flush the underlying data stream and then return the contained writer if the flush succeeded.
Note that this function may not be suitable to call in a situation where the underlying stream is an asynchronous I/O stream. To finish a stream the
try_finish(orshutdown) method should be used instead. To re-acquire ownership of a stream it is safe to call this method aftertry_finishorshutdownhas returnedOk.Errors
This function will perform I/O to complete this stream, and any I/O errors which occur will be returned from this function.
fn total_in(&self) -> u64Returns the number of bytes that the decompressor has consumed for decompression.
Note that this will likely be smaller than the number of bytes successfully written to this stream due to internal buffering.
fn total_out(&self) -> u64Returns the number of bytes that the decompressor has written to its output stream.
Trait Implementations
impl<W: Debug + Write> Debug for ZlibDecoder<W>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<W: Read + Write> Read for ZlibDecoder<W>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
impl<W: Write> Write for ZlibDecoder<W>
fn write(&mut self, buf: &[u8]) -> Result<usize>fn flush(&mut self) -> Result<()>