| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#[tokio::main] |
| 5 |
async fn main() { |
| 6 |
#[cfg(features = "deflate")] |
| 7 |
if let Err(err) = inner::run().await { |
| 8 |
eprintln!("Error: {}", err); |
| 9 |
eprintln!("Usage: cli_compress <input file or directory> <output ZIP file name>"); |
| 10 |
std::process::exit(1); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
#[cfg(features = "deflate")] |
| 15 |
mod inner { |
| 16 |
|
| 17 |
use async_zip::base::write::ZipFileWriter; |
| 18 |
use async_zip::{Compression, ZipEntryBuilder}; |
| 19 |
|
| 20 |
use std::path::{Path, PathBuf}; |
| 21 |
|
| 22 |
use anyhow::{anyhow, bail, Result}; |
| 23 |
use futures_lite::io::AsyncReadExt; |
| 24 |
use tokio::fs::File; |
| 25 |
|
| 26 |
async fn run() -> Result<()> { |
| 27 |
let mut args = std::env::args().skip(1); |
| 28 |
|
| 29 |
let input_str = args.next().ok_or(anyhow!("No input file or directory specified."))?; |
| 30 |
let input_path = Path::new(&input_str); |
| 31 |
|
| 32 |
let output_str = args.next().ok_or(anyhow!("No output file specified."))?; |
| 33 |
let output_path = Path::new(&output_str); |
| 34 |
|
| 35 |
let input_pathbuf = input_path.canonicalize().map_err(|_| anyhow!("Unable to canonicalise input path."))?; |
| 36 |
let input_path = input_pathbuf.as_path(); |
| 37 |
|
| 38 |
if output_path.exists() { |
| 39 |
bail!("The output file specified already exists."); |
| 40 |
} |
| 41 |
if !input_path.exists() { |
| 42 |
bail!("The input file or directory specified doesn't exist."); |
| 43 |
} |
| 44 |
|
| 45 |
let mut output_writer = ZipFileWriter::new(File::create(output_path).await?); |
| 46 |
|
| 47 |
if input_path.is_dir() { |
| 48 |
handle_directory(input_path, &mut output_writer).await?; |
| 49 |
} else { |
| 50 |
handle_singular(input_path, &mut output_writer).await?; |
| 51 |
} |
| 52 |
|
| 53 |
output_writer.close().await?; |
| 54 |
println!("Successfully written ZIP file '{}'.", output_path.display()); |
| 55 |
|
| 56 |
Ok(()) |
| 57 |
} |
| 58 |
|
| 59 |
async fn handle_singular(input_path: &Path, writer: &mut ZipFileWriter<File>) -> Result<()> { |
| 60 |
let filename = input_path.file_name().ok_or(anyhow!("Input path terminates in '...'."))?; |
| 61 |
let filename = filename.to_str().ok_or(anyhow!("Input path not valid UTF-8."))?; |
| 62 |
|
| 63 |
write_entry(filename, input_path, writer).await |
| 64 |
} |
| 65 |
|
| 66 |
async fn handle_directory(input_path: &Path, writer: &mut ZipFileWriter<File>) -> Result<()> { |
| 67 |
let entries = walk_dir(input_path.into()).await?; |
| 68 |
let input_dir_str = input_path.as_os_str().to_str().ok_or(anyhow!("Input path not valid UTF-8."))?; |
| 69 |
|
| 70 |
for entry_path_buf in entries { |
| 71 |
let entry_path = entry_path_buf.as_path(); |
| 72 |
let entry_str = entry_path.as_os_str().to_str().ok_or(anyhow!("Directory file path not valid UTF-8."))?; |
| 73 |
|
| 74 |
if !entry_str.starts_with(input_dir_str) { |
| 75 |
bail!("Directory file path does not start with base input directory path."); |
| 76 |
} |
| 77 |
|
| 78 |
let entry_str = &entry_str[input_dir_str.len() + 1..]; |
| 79 |
write_entry(entry_str, entry_path, writer).await?; |
| 80 |
} |
| 81 |
|
| 82 |
Ok(()) |
| 83 |
} |
| 84 |
|
| 85 |
async fn write_entry(filename: &str, input_path: &Path, writer: &mut ZipFileWriter<File>) -> Result<()> { |
| 86 |
let mut input_file = File::open(input_path).await?; |
| 87 |
let input_file_size = input_file.metadata().await?.len() as usize; |
| 88 |
|
| 89 |
let mut buffer = Vec::with_capacity(input_file_size); |
| 90 |
input_file.read_to_end(&mut buffer).await?; |
| 91 |
|
| 92 |
let builder = ZipEntryBuilder::new(filename.into(), Compression::Deflate); |
| 93 |
writer.write_entry_whole(builder, &buffer).await?; |
| 94 |
|
| 95 |
Ok(()) |
| 96 |
} |
| 97 |
|
| 98 |
async fn walk_dir(dir: PathBuf) -> Result<Vec<PathBuf>> { |
| 99 |
let mut dirs = vec![dir]; |
| 100 |
let mut files = vec![]; |
| 101 |
|
| 102 |
while !dirs.is_empty() { |
| 103 |
let mut dir_iter = tokio::fs::read_dir(dirs.remove(0)).await?; |
| 104 |
|
| 105 |
while let Some(entry) = dir_iter.next_entry().await? { |
| 106 |
let entry_path_buf = entry.path(); |
| 107 |
|
| 108 |
if entry_path_buf.is_dir() { |
| 109 |
dirs.push(entry_path_buf); |
| 110 |
} else { |
| 111 |
files.push(entry_path_buf); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
Ok(files) |
| 117 |
} |
| 118 |
} |