diff --git a/src/commands.rs b/src/commands.rs index 7467772..25abe96 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -326,8 +326,22 @@ fn decompress_file( Gzip | Bzip | Lzma | Zstd => { reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?; - // TODO: improve error treatment - let mut writer = fs::File::create(&output_path)?; + let mut writer = match fs::OpenOptions::new().write(true).create_new(true).open(&output_path) { + Ok(w) => w, + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => { + if utils::user_wants_to_overwrite(&output_path, question_policy)? { + if output_path.is_dir() { + // We can't just use `fs::File::create(&output_path)` because it would return io::ErrorKind::IsADirectory + // ToDo: Maybe we should emphasise that `output_path` is a directory and everything inside it will be gone? + fs::remove_dir_all(&output_path)?; + } + fs::File::create(&output_path)? + } else { + return Ok(()); + } + } + Err(e) => return Err(Error::from(e)), + }; io::copy(&mut reader, &mut writer)?; files_unpacked = vec![output_path];