use std::path::PathBuf;
use crate::{CliError, CliResult, extract_seek, list_seek};
#[derive(Debug, Default)]
struct UnzipOpts {
list: bool,
overwrite: bool,
dest: Option<String>,
password: Option<String>,
archive: Option<String>,
members: Vec<String>,
}
pub fn run_unzip(args: Vec<String>) -> CliResult {
match parse(args)? {
Parsed::Help => {
print!("{HELP}");
Ok(())
},
Parsed::Version => {
println!("oxunzip {}", env!("CARGO_PKG_VERSION"));
Ok(())
},
Parsed::Run(opts) => dispatch(&opts),
}
}
enum Parsed {
Run(UnzipOpts),
Help,
Version,
}
fn parse(args: Vec<String>) -> Result<Parsed, CliError> {
let mut opts = UnzipOpts::default();
let mut it = args.into_iter().peekable();
let mut only_positional = false;
while let Some(arg) = it.next() {
if only_positional {
push_operand(&mut opts, arg);
continue;
}
if arg == "--" {
only_positional = true;
continue;
}
if let Some(long) = arg.strip_prefix("--") {
match long {
"help" => return Ok(Parsed::Help),
"version" => return Ok(Parsed::Version),
other => return Err(CliError::usage(format!("unknown flag: --{other}"))),
}
}
if arg.starts_with('-') && arg.len() > 1 {
let cluster: Vec<char> = arg.chars().skip(1).collect();
let mut idx = 0;
while idx < cluster.len() {
let c = cluster[idx];
match c {
'l' => opts.list = true,
'o' => opts.overwrite = true,
'd' | 'P' => {
let rest: String = cluster[idx + 1..].iter().collect();
let value = if rest.is_empty() {
it.next()
.ok_or_else(|| CliError::usage(format!("-{c} requires a value")))?
} else {
rest
};
if c == 'd' {
opts.dest = Some(value);
} else {
opts.password = Some(value);
}
break;
},
'n' => {
return Err(CliError::unsupported(
"-n (never overwrite): not supported; extraction always overwrites",
));
},
'x' => {
return Err(CliError::unsupported(
"-x (exclude): not supported; select members positionally instead",
));
},
other => return Err(CliError::usage(format!("unknown flag: -{other}"))),
}
idx += 1;
}
continue;
}
push_operand(&mut opts, arg);
}
Ok(Parsed::Run(opts))
}
fn push_operand(opts: &mut UnzipOpts, arg: String) {
if opts.archive.is_none() {
opts.archive = Some(arg);
} else {
opts.members.push(arg);
}
}
fn dispatch(opts: &UnzipOpts) -> CliResult {
let archive = opts
.archive
.as_deref()
.ok_or_else(|| CliError::usage("missing zip archive operand. Try --help"))?;
let dest = opts
.dest
.as_deref()
.map_or_else(|| PathBuf::from("."), PathBuf::from);
let input = std::fs::File::open(archive)
.map_err(|error| CliError::runtime(format!("cannot read {archive}: {error}")))?;
let password = opts.password.as_deref().map(str::as_bytes);
if opts.list {
list_seek(input, password, &opts.members, false)
} else {
extract_seek(input, password, &dest, false, &opts.members, opts.overwrite)
}
}
const HELP: &str = "\
oxunzip: bsdunzip-compatible zip extractor
USAGE:
oxunzip [-o] [-d DIR] [-P PASSWORD] ARCHIVE.zip [MEMBER...]
oxunzip -l ARCHIVE.zip [MEMBER...]
OPTIONS:
-l List entries instead of extracting.
-d DIR Extract into DIR (default: current directory).
-o Atomically replace existing regular files (explicit restore opt-in).
-P PASSWORD Decryption password (WinZip AES-256 / zip64 supported).
--help, --version
UNSUPPORTED (exit 2): -n (never overwrite), -x (exclude), other classic flags.
SAFE DEFAULTS: path-traversal entries are refused and decompression is capped (untrusted input).
EXIT CODES: 0 success, 1 runtime failure, 2 usage/unsupported-flag error.
";