-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a5d7d4
commit 7f8311e
Showing
5 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::collections::VecDeque; | ||
use std::io::{copy, Error, Read, Result, Write}; | ||
use lzma_rs::decompress::{Options, Stream, UnpackedSize}; | ||
|
||
const COMPRESSED_BYTES_TO_BUFFER: usize = 4096; | ||
|
||
const OPTIONS: Options = Options { | ||
unpacked_size: UnpackedSize::ReadFromHeader, | ||
memlimit: None, | ||
allow_incomplete: true, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct LzmaReader<R> { | ||
compressed_reader: R, | ||
stream: Stream<VecDeque<u8>> | ||
} | ||
|
||
impl <R: Read> LzmaReader<R> { | ||
pub fn new(inner: R) -> Self { | ||
LzmaReader { | ||
compressed_reader: inner, | ||
stream: Stream::new_with_options(&OPTIONS, VecDeque::new()) | ||
} | ||
} | ||
|
||
pub fn finish(mut self) -> Result<VecDeque<u8>> { | ||
copy(&mut self.compressed_reader, &mut self.stream)?; | ||
self.stream.finish().map_err(Error::from) | ||
} | ||
} | ||
|
||
impl <R: Read> Read for LzmaReader<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
let mut bytes_read = self.stream.get_output_mut().unwrap().read(buf)?; | ||
while bytes_read < buf.len() { | ||
let mut next_compressed = [0u8; COMPRESSED_BYTES_TO_BUFFER]; | ||
let compressed_bytes_read = self.compressed_reader.read(&mut next_compressed)?; | ||
if compressed_bytes_read == 0 { | ||
break; | ||
} | ||
self.stream.write_all(&next_compressed[..compressed_bytes_read])?; | ||
bytes_read += self.stream.get_output_mut().unwrap().read(&mut buf[bytes_read..])?; | ||
} | ||
Ok(bytes_read) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters