mirror of
https://codeberg.org/icewind/logsmash.git
synced 2026-06-04 02:24:11 +02:00
add rar support
This commit is contained in:
parent
55444a3886
commit
14fb51c33d
6 changed files with 93 additions and 1 deletions
52
src/logfile/archive/rar.rs
Normal file
52
src/logfile/archive/rar.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use crate::error::ReadError;
|
||||
use crate::logfile::archive::{Archive, ArchiveEntry};
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub struct RarArchive {
|
||||
path: String,
|
||||
}
|
||||
|
||||
impl RarArchive {
|
||||
pub fn new(path: &str) -> Result<Self, ReadError> {
|
||||
Ok(RarArchive { path: path.into() })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RarEntry {
|
||||
archive: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl ArchiveEntry for RarEntry {
|
||||
fn name(&self) -> Cow<str> {
|
||||
self.name.as_str().into()
|
||||
}
|
||||
|
||||
fn extract(self) -> Result<Vec<u8>, ReadError> {
|
||||
let mut archive = unrar::Archive::new(&self.archive).open_for_processing()?;
|
||||
while let Some(header) = archive.read_header()? {
|
||||
if header.entry().filename.to_string_lossy() == self.name {
|
||||
return Ok(header.read()?.0);
|
||||
}
|
||||
archive = header.skip()?;
|
||||
}
|
||||
Err(ReadError::NoFiles)
|
||||
}
|
||||
}
|
||||
|
||||
impl Archive for RarArchive {
|
||||
type Entry<'a> = RarEntry;
|
||||
|
||||
fn entries(&mut self) -> impl Iterator<Item = Self::Entry<'_>> {
|
||||
unrar::Archive::new(&self.path)
|
||||
.open_for_listing()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|header| RarEntry {
|
||||
archive: self.path.clone(),
|
||||
name: header.filename.to_string_lossy().into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue