From 34ab32d2bb4081114537921054410af8de7521cd Mon Sep 17 00:00:00 2001 From: figsoda Date: Thu, 5 Nov 2020 11:24:14 -0500 Subject: [PATCH] add an option to specify the compression method --- Cargo.lock | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 6 +++++- src/main.rs | 23 +++++++++++++++++++-- src/release.rs | 3 ++- 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 601f870..59225ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,11 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + [[package]] name = "aho-corasick" version = "0.7.15" @@ -101,6 +107,27 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "bzip2" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42b7c3cbf0fa9c1b82308d57191728ca0256cb821220f4e2fd410a72ade26e3b" +dependencies = [ + "bzip2-sys", + "libc", +] + +[[package]] +name = "bzip2-sys" +version = "0.1.9+1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad3b39a260062fca31f7b0b12f207e8f2590a67d32ec7d59c20484b07ea7285e" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "cc" version = "1.0.61" @@ -206,6 +233,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "flate2" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cfff41391129e0a856d6d822600b8d71179d46879e310417eb9c762eb178b42" +dependencies = [ + "cfg-if 0.1.10", + "crc32fast", + "libc", + "miniz_oxide", +] + [[package]] name = "fnv" version = "1.0.7" @@ -596,6 +635,15 @@ dependencies = [ "unicase", ] +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + [[package]] name = "mio" version = "0.6.22" @@ -766,6 +814,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -1769,6 +1823,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "543adf038106b64cfca4711c82c917d785e3540e04f7996554488f988ec43124" dependencies = [ "byteorder", + "bzip2", "crc32fast", + "flate2", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index d75be7b..d676469 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,13 +26,17 @@ serde_json = "1.0.59" structopt = "0.3.20" toml = "0.5.7" walkdir = "2.3.1" -zip = { version = "0.5.8", default_features = false } [dependencies.reqwest] version = "0.10.8" default_features = false features = ["cookies", "json", "rustls-tls"] +[dependencies.zip] +version = "0.5.8" +default-features = false +features = ["bzip2", "deflate"] + [profile.release] lto = true codegen-units = 1 diff --git a/src/main.rs b/src/main.rs index b2a8f1b..814c407 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use rprompt::prompt_reply_stdout; use serde::Serialize; use structopt::{clap::AppSettings, StructOpt}; use walkdir::WalkDir; +use zip::CompressionMethod; use std::{ env::set_current_dir, @@ -31,6 +32,15 @@ struct Opts { #[structopt(short, long)] compact: bool, + /// Specify the compression method, ignored without `-z/--zip` flag + #[structopt( + long, + default_value = "stored", + possible_values(&["stored", "bz2", "deflate"]), + parse(from_str = compression_method), + )] + compression: CompressionMethod, + /// Set working directory #[structopt(short, long)] dir: Option, @@ -52,6 +62,15 @@ struct Opts { zip: bool, } +fn compression_method(compression: &str) -> CompressionMethod { + match compression { + "stored" => CompressionMethod::Stored, + "bz2" => CompressionMethod::Bzip2, + "deflate" => CompressionMethod::Deflated, + _ => unreachable!(), + } +} + #[tokio::main] async fn main() -> Result<()> { let opts = Opts::from_args(); @@ -108,7 +127,7 @@ async fn main() -> Result<()> { let file_name = &format!("{}_{}", cfg.package.name, cfg.package.version); if let Some(cred) = opts.publish { let mut zip = Cursor::new(Vec::with_capacity(256)); - release::zip(files, info, &mut zip, file_name.into())?; + release::zip(files, info, &mut zip, file_name.into(), opts.compression)?; if opts.zip { fs::create_dir_all(&opts.output).with_context(fail::create_dir(&opts.output))?; @@ -186,7 +205,7 @@ async fn main() -> Result<()> { release::remove_path(output)?; let file = File::create(output).with_context(fail::create_file(output.display()))?; - release::zip(files, info, file, file_name.into())?; + release::zip(files, info, file, file_name.into(), opts.compression)?; } else { let output = &Path::new(&opts.output).join(file_name); diff --git a/src/release.rs b/src/release.rs index 444cf87..e2cbfe1 100644 --- a/src/release.rs +++ b/src/release.rs @@ -42,12 +42,13 @@ pub fn zip( info: Vec, writer: impl Write + Seek, root: PathBuf, + compression: CompressionMethod, ) -> Result<()> { let mut zip = ZipWriter::new(writer); zip.set_comment(""); let fo = FileOptions::default() - .compression_method(CompressionMethod::Stored) + .compression_method(compression) .unix_permissions(0o755); for (from, to) in files {