1#[cfg(feature = "bzip")]
2pub use self::bzip2::Bzip2;
3
4#[cfg(feature = "bzip")]
5mod bzip2 {
6 use std::fs::{create_dir_all, File};
7 use std::io::{copy, Read};
8 use std::path::Path;
9
10 use bzip2::read::BzDecoder;
11
12 use crate::{Compressed, Result};
13
14 pub struct Bzip2<R: Read> {
15 archive: BzDecoder<R>,
16 }
17
18 impl Bzip2<File> {
19 pub fn open(path: &Path) -> std::io::Result<Self> {
20 let archive = File::open(path)?;
21
22 Self::new(archive)
23 }
24 }
25
26 impl<R: Read> Bzip2<R> {
27 pub fn new(r: R) -> std::io::Result<Self> {
28 let archive = BzDecoder::new(r);
29
30 Ok(Self { archive })
31 }
32 }
33
34 impl<R: Read> Compressed for Bzip2<R> {
35 fn decompress(&mut self, target: &Path) -> Result<()> {
36 if let Some(p) = target.parent() {
37 if !p.exists() {
38 create_dir_all(&p)?;
39 }
40 }
41
42 let mut output = File::create(target)?;
43 copy(&mut self.archive, &mut output)?;
44
45 Ok(())
46 }
47 }
48
49 impl<R: Read> Read for Bzip2<R> {
50 fn read(&mut self, into: &mut [u8]) -> std::io::Result<usize> {
51 self.archive.read(into)
52 }
53 }
54}