Struct DeflateDecoder
pub struct DeflateDecoder<W: Write> { /* private fields */ }
A DEFLATE 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 DEFLATE data this writer will return the number of bytes up to to the end of the DEFLATE member and subsequent writes will return Ok(0) allowing the caller to handle any data following the DEFLATE member.
Examples
use std::io::prelude::*;
use std::io;
# use flate2::Compression;
# use flate2::write::DeflateEncoder;
use flate2::write::DeflateDecoder;
# fn main() {
# let mut e = DeflateEncoder::new(Vec::new(), Compression::default());
# e.write_all(b"Hello World").unwrap();
# let bytes = e.finish().unwrap();
# println!("{}", decode_writer(bytes).unwrap());
# }
// Uncompresses a Deflate Encoded vector of bytes and returns a string or error
// Here Vec<u8> implements Write
fn decode_writer(bytes: Vec<u8>) -> io::Result<String> {
let mut writer = Vec::new();
let mut deflater = DeflateDecoder::new(writer);
deflater.write_all(&bytes[..])?;
writer = deflater.finish()?;
let return_string = String::from_utf8(writer).expect("String parsing error");
Ok(return_string)
}
Implementations
impl<W: Write> DeflateDecoder<W>
fn new(w: W) -> DeflateDecoder<W>Creates a new decoder which will write uncompressed data to the stream.
When this encoder 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 function will finish encoding the current stream into the current output stream before swapping out the two output streams.
This will then 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 finish the stream, and if that I/O returns an error then that 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 finish the stream, returning any errors which happen.
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 DeflateDecoder<W>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<W: Read + Write> Read for DeflateDecoder<W>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
impl<W: Write> Write for DeflateDecoder<W>
fn write(&mut self, buf: &[u8]) -> Result<usize>fn flush(&mut self) -> Result<()>