Prelim changes to write_dir
Remove old code and fix match statement Edit arg names and use PathBuf and Path Fix path ordering Fix enum names Add clap as a dev dependency Pin clap version
This commit is contained in:
parent
3e88fe66c9
commit
1852e96192
2 changed files with 81 additions and 47 deletions
|
@ -32,6 +32,7 @@ bencher = "0.1.5"
|
||||||
getrandom = "0.2.5"
|
getrandom = "0.2.5"
|
||||||
walkdir = "2.3.2"
|
walkdir = "2.3.2"
|
||||||
time = { version = "0.3.7", features = ["formatting", "macros"] }
|
time = { version = "0.3.7", features = ["formatting", "macros"] }
|
||||||
|
clap = { version = "=4.4.18", features = ["derive"] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
aes-crypto = [ "aes", "constant_time_eq", "hmac", "pbkdf2", "sha1" ]
|
aes-crypto = [ "aes", "constant_time_eq", "hmac", "pbkdf2", "sha1" ]
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use clap::{Parser, ValueEnum};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::{Seek, Write};
|
use std::io::{Seek, Write};
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
|
@ -5,66 +6,98 @@ use zip::result::ZipError;
|
||||||
use zip::write::FileOptions;
|
use zip::write::FileOptions;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
use walkdir::{DirEntry, WalkDir};
|
use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
// Source directory
|
||||||
|
source: PathBuf,
|
||||||
|
// Destination zipfile
|
||||||
|
destination: PathBuf,
|
||||||
|
// Compression method
|
||||||
|
#[arg(value_enum)]
|
||||||
|
compression_method: CompressionMethod,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, ValueEnum)]
|
||||||
|
enum CompressionMethod {
|
||||||
|
Stored,
|
||||||
|
Deflated,
|
||||||
|
DeflatedMiniz,
|
||||||
|
DeflatedZlib,
|
||||||
|
Bzip2,
|
||||||
|
Zstd,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
std::process::exit(real_main());
|
std::process::exit(real_main());
|
||||||
}
|
}
|
||||||
|
|
||||||
const METHOD_STORED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Stored);
|
|
||||||
|
|
||||||
#[cfg(any(
|
|
||||||
feature = "deflate",
|
|
||||||
feature = "deflate-miniz",
|
|
||||||
feature = "deflate-zlib"
|
|
||||||
))]
|
|
||||||
const METHOD_DEFLATED: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Deflated);
|
|
||||||
#[cfg(not(any(
|
|
||||||
feature = "deflate",
|
|
||||||
feature = "deflate-miniz",
|
|
||||||
feature = "deflate-zlib"
|
|
||||||
)))]
|
|
||||||
const METHOD_DEFLATED: Option<zip::CompressionMethod> = None;
|
|
||||||
|
|
||||||
#[cfg(feature = "bzip2")]
|
|
||||||
const METHOD_BZIP2: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Bzip2);
|
|
||||||
#[cfg(not(feature = "bzip2"))]
|
|
||||||
const METHOD_BZIP2: Option<zip::CompressionMethod> = None;
|
|
||||||
|
|
||||||
#[cfg(feature = "zstd")]
|
|
||||||
const METHOD_ZSTD: Option<zip::CompressionMethod> = Some(zip::CompressionMethod::Zstd);
|
|
||||||
#[cfg(not(feature = "zstd"))]
|
|
||||||
const METHOD_ZSTD: Option<zip::CompressionMethod> = None;
|
|
||||||
|
|
||||||
fn real_main() -> i32 {
|
fn real_main() -> i32 {
|
||||||
let args: Vec<_> = std::env::args().collect();
|
let args = Args::parse();
|
||||||
if args.len() < 3 {
|
let src_dir = &args.source;
|
||||||
println!(
|
let dst_file = &args.destination;
|
||||||
"Usage: {} <source_directory> <destination_zipfile>",
|
let method = match args.compression_method {
|
||||||
args[0]
|
CompressionMethod::Stored => zip::CompressionMethod::Stored,
|
||||||
);
|
CompressionMethod::Deflated => {
|
||||||
|
#[cfg(not(feature = "deflate"))]
|
||||||
|
{
|
||||||
|
println!("The `deflate` feature is not enabled");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "deflate")]
|
||||||
let src_dir = &*args[1];
|
zip::CompressionMethod::Deflated
|
||||||
let dst_file = &*args[2];
|
},
|
||||||
for &method in [METHOD_STORED, METHOD_DEFLATED, METHOD_BZIP2, METHOD_ZSTD].iter() {
|
CompressionMethod::DeflatedMiniz => {
|
||||||
if method.is_none() {
|
#[cfg(not(feature = "deflate-miniz"))]
|
||||||
continue;
|
{
|
||||||
|
println!("The `deflate-miniz` feature is not enabled");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
match doit(src_dir, dst_file, method.unwrap()) {
|
#[cfg(feature = "deflate-miniz")]
|
||||||
Ok(_) => println!("done: {src_dir} written to {dst_file}"),
|
zip::CompressionMethod::Deflated
|
||||||
|
},
|
||||||
|
CompressionMethod::DeflatedZlib => {
|
||||||
|
#[cfg(not(feature = "deflate-zlib"))]
|
||||||
|
{
|
||||||
|
println!("The `deflate-zlib` feature is not enabled");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#[cfg(feature = "deflate-zlib")]
|
||||||
|
zip::CompressionMethod::Deflated
|
||||||
|
},
|
||||||
|
CompressionMethod::Bzip2 => {
|
||||||
|
#[cfg(not(feature = "bzip2"))]
|
||||||
|
{
|
||||||
|
println!("The `bzip2` feature is not enabled");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#[cfg(feature = "bzip2")]
|
||||||
|
zip::CompressionMethod::Bzip2
|
||||||
|
},
|
||||||
|
CompressionMethod::Zstd => {
|
||||||
|
#[cfg(not(feature = "zstd"))]
|
||||||
|
{
|
||||||
|
println!("The `zstd` feature is not enabled");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#[cfg(feature = "zstd")]
|
||||||
|
zip::CompressionMethod::Zstd
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match doit(src_dir, dst_file, method) {
|
||||||
|
Ok(_) => println!("done: {:?} written to {:?}", src_dir, dst_file),
|
||||||
Err(e) => println!("Error: {e:?}"),
|
Err(e) => println!("Error: {e:?}"),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zip_dir<T>(
|
fn zip_dir<T>(
|
||||||
it: &mut dyn Iterator<Item = DirEntry>,
|
it: &mut dyn Iterator<Item = DirEntry>,
|
||||||
prefix: &str,
|
prefix: &Path,
|
||||||
writer: T,
|
writer: T,
|
||||||
method: zip::CompressionMethod,
|
method: zip::CompressionMethod,
|
||||||
) -> zip::result::ZipResult<()>
|
) -> zip::result::ZipResult<()>
|
||||||
|
@ -105,8 +138,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn doit(
|
fn doit(
|
||||||
src_dir: &str,
|
src_dir: &Path,
|
||||||
dst_file: &str,
|
dst_file: &Path,
|
||||||
method: zip::CompressionMethod,
|
method: zip::CompressionMethod,
|
||||||
) -> zip::result::ZipResult<()> {
|
) -> zip::result::ZipResult<()> {
|
||||||
if !Path::new(src_dir).is_dir() {
|
if !Path::new(src_dir).is_dir() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue