ripgrep-all/src/adapters/zip.rs

87 lines
2.5 KiB
Rust
Raw Normal View History

2019-06-06 09:00:13 +00:00
use super::*;
use crate::preproc::rga_preproc;
2019-06-06 14:48:31 +00:00
use ::zip::read::ZipFile;
2019-06-06 09:00:13 +00:00
use failure::*;
use lazy_static::lazy_static;
2019-06-06 21:43:30 +00:00
2019-06-06 09:00:13 +00:00
// todo:
// maybe todo: read list of extensions from
//ffmpeg -demuxers | tail -n+5 | awk '{print $2}' | while read demuxer; do echo MUX=$demuxer; ffmpeg -h demuxer=$demuxer | grep 'Common extensions'; done 2>/dev/null
static EXTENSIONS: &[&str] = &["zip"];
lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "zip".to_owned(),
version: 1,
matchers: EXTENSIONS
.iter()
.map(|s| Matcher::FileExtension(s.to_string()))
.collect(),
};
}
2019-06-06 21:43:30 +00:00
#[derive(Default)]
2019-06-06 09:00:13 +00:00
pub struct ZipAdapter;
impl ZipAdapter {
pub fn new() -> ZipAdapter {
ZipAdapter
}
}
impl GetMetadata for ZipAdapter {
2019-06-06 21:43:30 +00:00
fn metadata(&self) -> &AdapterMeta {
2019-06-06 09:00:13 +00:00
&METADATA
}
}
2019-06-06 14:48:31 +00:00
// https://github.com/mvdnes/zip-rs/commit/b9af51e654793931af39f221f143b9dea524f349
fn is_dir(f: &ZipFile) -> bool {
f.name()
.chars()
.rev()
.next()
.map_or(false, |c| c == '/' || c == '\\')
}
2019-06-06 09:00:13 +00:00
impl FileAdapter for ZipAdapter {
fn adapt(&self, ai: AdaptInfo) -> Fallible<()> {
let AdaptInfo {
filepath_hint,
mut inp,
oup,
line_prefix,
2019-06-07 13:43:19 +00:00
archive_recursion_depth,
2019-06-07 17:00:24 +00:00
config,
2019-06-06 09:00:13 +00:00
..
} = ai;
loop {
match ::zip::read::read_zipfile_from_stream(&mut inp) {
Ok(None) => break,
Ok(Some(mut file)) => {
2019-06-06 14:48:31 +00:00
if is_dir(&file) {
continue;
}
2019-06-06 09:00:13 +00:00
eprintln!(
"{}|{}: {} bytes ({} bytes packed)",
filepath_hint.to_string_lossy(),
file.name(),
file.size(),
file.compressed_size()
);
2019-06-06 21:43:30 +00:00
let line_prefix = &format!("{}{}: ", line_prefix, file.name());
2019-06-07 17:00:24 +00:00
rga_preproc(AdaptInfo {
filepath_hint: &file.sanitized_name(),
is_real_file: false,
inp: &mut file,
oup,
line_prefix,
archive_recursion_depth: archive_recursion_depth + 1,
config,
})?;
2019-06-06 09:00:13 +00:00
}
Err(e) => return Err(e.into()),
}
}
Ok(())
}
}