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 GzBuilder type.

The data written to the returned encoder will be compressed and then written to the stream w.

fn get_ref(&self) -> &W

Acquires a reference to the underlying writer.

fn get_mut(&mut self) -> &mut W

Acquires 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 applying std::mem::take to get_mut when forwarding the stream incrementally.

To start a new stream, call finish and 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 write may 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 (or shutdown) method should be used instead. To re-acquire ownership of a stream it is safe to call this method after try_finish or shutdown has returned Ok.

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<()>

Auto Trait Implementations

impl<W> !UnwindSafe for GzEncoder<W>

impl<W> Freeze for GzEncoder<W> where Writer<W, Compress>: Freeze,

impl<W> RefUnwindSafe for GzEncoder<W> where Writer<W, Compress>: RefUnwindSafe,

impl<W> Send for GzEncoder<W> where Writer<W, Compress>: Send,

impl<W> Sync for GzEncoder<W> where Writer<W, Compress>: Sync,

impl<W> Unpin for GzEncoder<W> where Writer<W, Compress>: Unpin,

impl<W> UnsafeUnpin for GzEncoder<W> where Writer<W, Compress>: UnsafeUnpin,

Blanket Implementations

impl<T> Any for GzEncoder<W> where T: 'static + ?Sized,

impl<T> Borrow<T> for GzEncoder<W> where T: ?Sized,

impl<T> BorrowMut<T> for GzEncoder<W> where T: ?Sized,

impl<T> From<T> for GzEncoder<W>

impl<T, U> Into<U> for GzEncoder<W> where U: From<T>,

impl<T, U> TryFrom<U> for GzEncoder<W> where U: Into<T>,

impl<T, U> TryInto<U> for GzEncoder<W> where U: TryFrom<T>,