/// A gzip streaming encoder /// /// This structure implements a [`Read`] interface. When read from, it reads /// uncompressed data from the underlying [`Read`] and provides the compressed data. /// /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html /// /// # Examples /// /// ``` /// use std::io::prelude::*; /// use std::io; /// use flate2::Compression; /// use flate2::read::GzEncoder; /// /// // Return a vector containing the GZ compressed version of hello world /// /// fn gzencode_hello_world() -> io::Result<Vec<u8>> { /// let mut ret_vec = Vec::new(); /// let bytestring = b"hello world"; /// let mut gz = GzEncoder::new(&bytestring[..], Compression::fast()); /// gz.read_to_end(&mut ret_vec)?; /// Ok(ret_vec) /// } /// ``` #[derive(Debug)] pubstruct GzEncoder<R> {
inner: bufread::GzEncoder<BufReader<R>>,
}
impl<R: Read> GzEncoder<R> { /// 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 read from the stream `r` will be compressed and available /// through the returned reader. pubfn new(r: R, level: Compression) -> GzEncoder<R> {
GzBuilder::new().read(r, level)
}
}
impl<R> GzEncoder<R> { /// Acquires a reference to the underlying reader. pubfn get_ref(&self) -> &R { self.inner.get_ref().get_ref()
}
/// Acquires a mutable reference to the underlying reader. /// /// Note that mutation of the reader may result in surprising results if /// this encoder is continued to be used. pubfn get_mut(&mutself) -> &mut R { self.inner.get_mut().get_mut()
}
/// Returns the underlying stream, consuming this encoder pubfn into_inner(self) -> R { self.inner.into_inner().into_inner()
}
}
/// A decoder for a single member of a [gzip file]. /// /// This structure implements a [`Read`] interface. When read from, it reads /// compressed data from the underlying [`Read`] and provides the uncompressed data. /// /// After reading a single member of the gzip data this reader will return /// Ok(0) even if there are more bytes available in the underlying reader. /// `GzDecoder` may have read additional bytes past the end of the gzip data. /// If you need the following bytes, wrap the `Reader` in a `std::io::BufReader` /// and use `bufread::GzDecoder` instead. /// /// To handle gzip files that may have multiple members, see [`MultiGzDecoder`] /// or read more /// [in the introduction](../index.html#about-multi-member-gzip-files). /// /// [gzip file]: https://www.rfc-editor.org/rfc/rfc1952#page-5 /// /// # Examples /// /// ``` /// use std::io::prelude::*; /// use std::io; /// # use flate2::Compression; /// # use flate2::write::GzEncoder; /// use flate2::read::GzDecoder; /// /// # fn main() { /// # let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// # e.write_all(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); /// # } /// # /// // Uncompresses a Gz Encoded vector of bytes and returns a string or error /// // Here &[u8] implements Read /// /// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { /// let mut gz = GzDecoder::new(&bytes[..]); /// let mut s = String::new(); /// gz.read_to_string(&mut s)?; /// Ok(s) /// } /// ``` #[derive(Debug)] pubstruct GzDecoder<R> {
inner: bufread::GzDecoder<BufReader<R>>,
}
impl<R: Read> GzDecoder<R> { /// Creates a new decoder from the given reader, immediately parsing the /// gzip header. pubfn new(r: R) -> GzDecoder<R> {
GzDecoder {
inner: bufread::GzDecoder::new(BufReader::new(r)),
}
}
}
impl<R> GzDecoder<R> { /// Returns the header associated with this stream, if it was valid. pubfn header(&self) -> Option<&GzHeader> { self.inner.header()
}
/// Acquires a reference to the underlying reader. /// /// Note that the decoder may have read past the end of the gzip data. /// To prevent this use [`bufread::GzDecoder`] instead. pubfn get_ref(&self) -> &R { self.inner.get_ref().get_ref()
}
/// Acquires a mutable reference to the underlying stream. /// /// Note that mutation of the stream may result in surprising results if /// this decoder continues to be used. /// /// Note that the decoder may have read past the end of the gzip data. /// To prevent this use [`bufread::GzDecoder`] instead. pubfn get_mut(&mutself) -> &mut R { self.inner.get_mut().get_mut()
}
/// Consumes this decoder, returning the underlying reader. /// /// Note that the decoder may have read past the end of the gzip data. /// Subsequent reads will skip those bytes. To prevent this use /// [`bufread::GzDecoder`] instead. pubfn into_inner(self) -> R { self.inner.into_inner().into_inner()
}
}
/// A gzip streaming decoder that decodes a [gzip file] that may have multiple members. /// /// This structure implements a [`Read`] interface. When read from, it reads /// compressed data from the underlying [`Read`] and provides the uncompressed /// data. /// /// A gzip file consists of a series of *members* concatenated one after another. /// MultiGzDecoder decodes all members of a file and returns Ok(0) once the /// underlying reader does. /// /// To handle members separately, see [GzDecoder] or read more /// [in the introduction](../index.html#about-multi-member-gzip-files). /// /// [gzip file]: https://www.rfc-editor.org/rfc/rfc1952#page-5 /// /// # Examples /// /// ``` /// use std::io::prelude::*; /// use std::io; /// # use flate2::Compression; /// # use flate2::write::GzEncoder; /// use flate2::read::MultiGzDecoder; /// /// # fn main() { /// # let mut e = GzEncoder::new(Vec::new(), Compression::default()); /// # e.write_all(b"Hello World").unwrap(); /// # let bytes = e.finish().unwrap(); /// # println!("{}", decode_reader(bytes).unwrap()); /// # } /// # /// // Uncompresses a Gz Encoded vector of bytes and returns a string or error /// // Here &[u8] implements Read /// /// fn decode_reader(bytes: Vec<u8>) -> io::Result<String> { /// let mut gz = MultiGzDecoder::new(&bytes[..]); /// let mut s = String::new(); /// gz.read_to_string(&mut s)?; /// Ok(s) /// } /// ``` #[derive(Debug)] pubstruct MultiGzDecoder<R> {
inner: bufread::MultiGzDecoder<BufReader<R>>,
}
impl<R: Read> MultiGzDecoder<R> { /// Creates a new decoder from the given reader, immediately parsing the /// (first) gzip header. If the gzip stream contains multiple members all will /// be decoded. pubfn new(r: R) -> MultiGzDecoder<R> {
MultiGzDecoder {
inner: bufread::MultiGzDecoder::new(BufReader::new(r)),
}
}
}
impl<R> MultiGzDecoder<R> { /// Returns the current header associated with this stream, if it's valid. pubfn header(&self) -> Option<&GzHeader> { self.inner.header()
}
/// Acquires a reference to the underlying reader. pubfn get_ref(&self) -> &R { self.inner.get_ref().get_ref()
}
/// Acquires a mutable reference to the underlying stream. /// /// Note that mutation of the stream may result in surprising results if /// this decoder is continued to be used. pubfn get_mut(&mutself) -> &mut R { self.inner.get_mut().get_mut()
}
/// Consumes this decoder, returning the underlying reader. pubfn into_inner(self) -> R { self.inner.into_inner().into_inner()
}
}
impl Read for BlockingCursor { fn read(&mutself, buf: &mut [u8]) -> Result<usize> { //use the cursor, except it turns eof into blocking error let r = self.cursor.read(buf); match r {
Err(ref err) => { if err.kind() == ErrorKind::UnexpectedEof { return Err(ErrorKind::WouldBlock.into());
}
}
Ok(0) => { //regular EOF turned into blocking error return Err(ErrorKind::WouldBlock.into());
}
Ok(_n) => {}
} return r;
}
}
#[test] fn blocked_partial_header_read() { // this is a reader which receives data afterwards letmut r = BlockingCursor::new(); let data = vec![1, 2, 3];
match r.write_all(&data) {
Ok(()) => {}
_ => {
panic!("Unexpected result for write_all");
}
}
r.set_position(0);
// this is unused except for the buffering letmut decoder = GzDecoder::new(r); letmut out = Vec::with_capacity(7); match decoder.read(&mut out) {
Err(e) => {
assert_eq!(e.kind(), ErrorKind::WouldBlock);
}
_ => {
panic!("Unexpected result for decoder.read");
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.20 Sekunden
(vorverarbeitet am 2026-06-20)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.