use anyhow::Result;
use clap::Args;
use std::fs::File;
use std::{
io::{BufReader, BufWriter},
path::PathBuf,
};
use zip::ZipArchive;
use log::debug;
pub const DEFAULT_PATH_TEMP: &str = "temp";
#[derive(Args)]
pub struct ExtractCMD {
#[arg(short = 'i', value_parser)] pub path_in: PathBuf,
#[arg(short = 'o', value_parser)] pub path_out: PathBuf,
#[arg(short = 't', value_parser= clap::value_parser!(PathBuf), default_value = DEFAULT_PATH_TEMP)] pub path_tmp: PathBuf,
#[arg(short = 'b', value_parser)] pub cell_id: String,
#[arg(short = 'f', value_parser)] pub fname: String,
}
impl ExtractCMD {
pub fn try_execute(&mut self) -> Result<()> {
let file = File::open(&self.path_in).expect("Failed to open bascet shard");
let bufreader_shard = BufReader::new(file);
let mut zip_shard = ZipArchive::new(bufreader_shard).unwrap();
let zip_fname = format!("{}/{}",self.cell_id, self.fname);
let mut entry = zip_shard.by_name(&zip_fname).expect("File is not present");
if entry.is_file() {
debug!("extracting {} ", self.fname);
let file_out = File::create(&self.path_out).unwrap();
let mut bufwriter_out = BufWriter::new(&file_out);
let mut bufreader_found = BufReader::new(&mut entry);
std::io::copy(&mut bufreader_found, &mut bufwriter_out).unwrap();
}
log::info!("Extract has finished succesfully");
Ok(())
}
}