support .zstd, .xz and .bz2 compressed inputs
Some checks are pending
CI / matrix (push) Waiting to run
CI / ${{ matrix.check }} (push) Blocked by required conditions
CI / build (push) Blocked by required conditions
CI / build-nixpkgs (push) Blocked by required conditions

This commit is contained in:
Robin Appelman 2024-11-05 17:21:14 +01:00
commit 4f3d6a17ab
4 changed files with 80 additions and 3 deletions

View file

@ -1,3 +1,4 @@
use ruzstd::frame_decoder::FrameDecoderError;
use std::string::FromUtf8Error;
use thiserror::Error;
use zip::result::ZipError;
@ -22,6 +23,8 @@ pub enum ReadError {
Io(#[from] std::io::Error),
#[error(transparent)]
Zip(#[from] ZipError),
#[error(transparent)]
Zstd(#[from] FrameDecoderError),
#[error("archive contains multiple files")]
MultipleFiles,
#[error("archive contains no files")]

View file

@ -2,9 +2,12 @@ mod archive;
use crate::error::ReadError;
use crate::logfile::archive::{Archive, ArchiveEntry, TarArchive, ZipArchive};
use bzip2_rs::DecoderReader;
use flate2::read::GzDecoder;
use ruzstd::StreamingDecoder;
use std::fs::File;
use std::io::Read;
use std::io::{BufReader, Read};
use xz2::read::XzDecoder;
pub struct LogFile {
content: String,
@ -13,6 +16,7 @@ pub struct LogFile {
impl LogFile {
pub fn open(path: &str) -> Result<LogFile, ReadError> {
let file = File::open(path)?;
let file = BufReader::new(file);
if path.ends_with(".zip") {
let mut zip = ZipArchive::new(file)?;
let content = select_file(&mut zip)?;
@ -23,6 +27,15 @@ impl LogFile {
if let Some(path) = path.strip_suffix(".gz") {
let decoder = GzDecoder::new(file);
return Self::open_no_seek(path, decoder);
} else if let Some(path) = path.strip_suffix(".xz") {
let decoder = XzDecoder::new(file);
return Self::open_no_seek(path, decoder);
} else if let Some(path) = path.strip_suffix(".bz2") {
let decoder = DecoderReader::new(file);
return Self::open_no_seek(path, decoder);
} else if let Some(path) = path.strip_suffix(".zst") {
let decoder = StreamingDecoder::new(file)?;
return Self::open_no_seek(path, decoder);
}
Self::open_no_seek(path, Box::new(file))