Skip to content

Commit

Permalink
add an option to specify the compression method
Browse files Browse the repository at this point in the history
  • Loading branch information
figsoda committed Nov 5, 2020
1 parent f95ca67 commit 34ab32d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String>,
Expand All @@ -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();
Expand Down Expand Up @@ -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))?;
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ pub fn zip(
info: Vec<u8>,
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 {
Expand Down

0 comments on commit 34ab32d

Please sign in to comment.