singlefile_formats/compression/
bzip.rs

1#![cfg_attr(docsrs, doc(cfg(feature = "bzip")))]
2#![cfg(feature = "bzip")]
3
4//! Defines a [`CompressionFormat`] for the bzip compression algorithm.
5
6pub extern crate bzip2 as original;
7
8use crate::compression::{CompressionFormat, CompressionFormatLevels};
9
10use std::io::{Read, Write};
11
12/// A [`CompressionFormat`] corresponding to the bzip compression algorithm.
13/// Implemented using the [`bzip2`] crate.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub struct BZip2;
16
17impl CompressionFormat for BZip2 {
18  type Encoder<W: Write> = bzip2::write::BzEncoder::<W>;
19  type Decoder<R: Read> = bzip2::read::BzDecoder::<R>;
20
21  fn encode_writer<W: Write>(&self, writer: W, level: u32) -> Self::Encoder<W> {
22    Self::Encoder::new(writer, bzip2::Compression::new(level))
23  }
24
25  fn decode_reader<R: Read>(&self, reader: R) -> Self::Decoder<R> {
26    Self::Decoder::new(reader)
27  }
28}
29
30impl CompressionFormatLevels for BZip2 {
31  const COMPRESSION_LEVEL_NONE: u32 = 0;
32  const COMPRESSION_LEVEL_FAST: u32 = 1;
33  const COMPRESSION_LEVEL_BEST: u32 = 9;
34  const COMPRESSION_LEVEL_DEFAULT: u32 = 6;
35}