Struct GzEncoder
pub struct GzEncoder<W: Write> { /* private fields */ }
A gzip streaming encoder
This structure exposes a Write interface that will emit compressed data
to the underlying writer W.
Examples
use std::io::prelude::*;
use flate2::Compression;
use flate2::write::GzEncoder;
// Vec<u8> implements Write to print the compressed bytes of sample string
# fn main() {
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(b"Hello World").unwrap();
println!("{:?}", e.finish().unwrap());
# }
Implementations
impl<W: Write> GzEncoder<W>
fn new(w: W, level: Compression) -> GzEncoder<W>Creates a new encoder which will use the given compression level.
The encoder is not configured specially for the emitted header. For header configuration, see the
GzBuildertype.The data written to the returned encoder will be compressed and then written to the stream
w.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 when all input accepted so far must be decodable without output from later writes. This inserts a sync-flush point and changes the output bitstream. This is useful before applyingstd::mem::taketoget_mutwhen forwarding the stream incrementally.To start a new stream, call
finishand create a new encoder; replacing the writer does not reset it.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>Finish encoding this stream, returning the underlying writer once the encoding is done.
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.
Trait Implementations
impl<R: Read + Write> Read for GzEncoder<R>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
impl<W: Debug + Write> Debug for GzEncoder<W>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<W: Write> Drop for GzEncoder<W>
fn drop(&mut self)
impl<W: Write> Write for GzEncoder<W>
fn write(&mut self, buf: &[u8]) -> Result<usize>fn flush(&mut self) -> Result<()>