ripgrep-all/src/adapters/zip.rs

86 lines
2.4 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;
use std::fs::File;
// 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(),
};
}
pub struct ZipAdapter;
impl ZipAdapter {
pub fn new() -> ZipAdapter {
ZipAdapter
}
}
impl GetMetadata for ZipAdapter {
fn metadata<'a>(&'a self) -> &'a AdapterMeta {
&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<()> {
use std::io::prelude::*;
let AdaptInfo {
filepath_hint,
mut inp,
oup,
line_prefix,
..
} = 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 12:55:27 +00:00
let line_prefix = &format!("{}{}: ", line_prefix, file.name().clone());
2019-06-06 09:00:13 +00:00
rga_preproc(
AdaptInfo {
filepath_hint: &file.sanitized_name(),
inp: &mut file,
oup: oup,
line_prefix,
},
None,
)?;
}
Err(e) => return Err(e.into()),
}
}
Ok(())
}
}