Add detection for zstd compression. Reader must implement Read + Seek
This commit is contained in:
parent
3269da7fe8
commit
2c0fcc47a0
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/target
|
||||
test/
|
||||
!test/li.txt
|
||||
!test/li.txt.zst
|
||||
|
@ -38,4 +38,4 @@ to see Haggis implemented in other languages.
|
||||
- [x] List archive nodes
|
||||
- [x] Override user/group when creating archives
|
||||
- [x] Override user/group when extracting archives
|
||||
- [ ] Automatically detect zstd compressed archives
|
||||
- [x] Automatically detect zstd compressed archives
|
||||
|
25
src/lib.rs
25
src/lib.rs
@ -3,7 +3,7 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
io::{BufWriter, Write},
|
||||
io::{BufWriter, Read, Seek, Write},
|
||||
sync::Mutex,
|
||||
};
|
||||
#[cfg(feature = "parallel")]
|
||||
@ -41,6 +41,18 @@ pub use stream::Message as StreamMessage;
|
||||
|
||||
pub static MAGIC: [u8; 7] = [0x89, b'h', b'a', b'g', b'g', b'i', b's'];
|
||||
|
||||
static ZSTD_MAGIC: [u8; 4] = [0x28, 0xb5, 0x2f, 0xfd];
|
||||
|
||||
/// Tries to detect if the bytes in *reader* are zstd compressed
|
||||
/// # Errors
|
||||
/// Returns `Error` if io fails
|
||||
pub fn detect_zstd<R: Read + Seek>(reader: &mut R) -> Result<bool, Error> {
|
||||
let mut buf = [0; 4];
|
||||
reader.read_exact(&mut buf)?;
|
||||
reader.rewind()?;
|
||||
Ok(buf == ZSTD_MAGIC)
|
||||
}
|
||||
|
||||
/// Creates a haggis archive from a list of files
|
||||
/// # Errors
|
||||
/// Returns `crate::Error` if io fails or several other error conditions
|
||||
@ -176,3 +188,14 @@ pub fn par_stream_archive<W: Write + Send>(
|
||||
sender.send(Message::Eof).map_err(|_| Error::SenderError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn check_zstd() {
|
||||
let mut fd = fs::File::open("test/li.txt.zst").unwrap();
|
||||
assert!(detect_zstd(&mut fd).unwrap());
|
||||
}
|
||||
}
|
||||
|
BIN
test/li.txt.zst
Normal file
BIN
test/li.txt.zst
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user