Add detection for zstd compression. Reader must implement Read + Seek

This commit is contained in:
Nathan Fisher 2024-01-23 00:07:03 -05:00
parent 3269da7fe8
commit 2c0fcc47a0
4 changed files with 26 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target /target
test/ test/
!test/li.txt !test/li.txt
!test/li.txt.zst

View File

@ -38,4 +38,4 @@ to see Haggis implemented in other languages.
- [x] List archive nodes - [x] List archive nodes
- [x] Override user/group when creating archives - [x] Override user/group when creating archives
- [x] Override user/group when extracting archives - [x] Override user/group when extracting archives
- [ ] Automatically detect zstd compressed archives - [x] Automatically detect zstd compressed archives

View File

@ -3,7 +3,7 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs, fs,
io::{BufWriter, Write}, io::{BufWriter, Read, Seek, Write},
sync::Mutex, sync::Mutex,
}; };
#[cfg(feature = "parallel")] #[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']; 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 /// Creates a haggis archive from a list of files
/// # Errors /// # Errors
/// Returns `crate::Error` if io fails or several other error conditions /// 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)?; sender.send(Message::Eof).map_err(|_| Error::SenderError)?;
Ok(()) 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

Binary file not shown.