Skip to content

Commit

Permalink
Compress file when zipping, print errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Apr 18, 2021
1 parent 0eb0f98 commit ead5c84
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ dyn-fmt = "0.3.0"
colored = "2.0.0"
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"
zip = { version = "0.5.12", default-features = false }
zip = "0.5.12"
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ fn main() {
let path = Path::new(&file_path);
let res = workspace::zip_file(path);

if res.is_none() {
println!("Error zipping {}!", path.to_str().unwrap());
if let Err(e) = res {
println!(
"Error zipping {}! Error: {}",
path.to_str().unwrap(),
e.to_string().bright_red()
);
return;
}

Expand Down
22 changes: 11 additions & 11 deletions src/workspace/zip.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::fs::{read, File};
use std::io::Write;
use std::io::{Error, ErrorKind, Write};
use std::path::Path;

pub fn zip_file(path: &Path) -> Option<String> {
let filename = path.file_name().unwrap().to_str()?;
let path = path.to_str()?;
pub fn zip_file(path: &Path) -> Result<String, Error> {
let filename = path.file_name().unwrap().to_str().ok_or(ErrorKind::Other)?;
let path = path.to_str().ok_or(ErrorKind::Other)?;

println!("Zipping {}.", filename);
tracing::debug!("Relative path: {}.", path);

let source = read(path).ok()?;
let buf = File::create("source.zip").ok()?;
let source = read(path)?;
let buf = File::create("source.zip")?;
let mut zip = zip::ZipWriter::new(buf);

let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
zip.start_file(filename, options).ok()?;
zip.write(source.as_ref()).ok()?;
zip.finish().ok()?;
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::DEFLATE);
zip.start_file(filename, options)?;
zip.write_all(source.as_ref())?;
zip.finish()?;

Some("source.zip".to_string())
Ok("source.zip".to_string())
}

0 comments on commit ead5c84

Please sign in to comment.