diff --git a/app/src/main/java/io/github/zchunk/app/commands/Unzck.java b/app/src/main/java/io/github/zchunk/app/commands/Unzck.java index 51ede6a..972b6e4 100644 --- a/app/src/main/java/io/github/zchunk/app/commands/Unzck.java +++ b/app/src/main/java/io/github/zchunk/app/commands/Unzck.java @@ -29,9 +29,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.SortedSet; import java.util.StringJoiner; import java.util.concurrent.Callable; +import java.util.logging.Level; import java.util.logging.Logger; import org.checkerframework.checker.nullness.qual.Nullable; import picocli.CommandLine.Command; @@ -91,8 +93,10 @@ private int decompressFile(final ZChunkFile zChunkFile) { uncompressChunks(fileOutputStream, zChunkFileHeader, decompressedDict); } catch (final FileNotFoundException fnfe) { + cleanPartialFile(target); throw new UncompressException("Unable to create parent dir or file: [" + target.getAbsolutePath() + "].", fnfe); } catch (final IOException ex) { + cleanPartialFile(target); throw new UncompressException("Unable to write file: [" + target.getAbsolutePath() + "].", ex); } @@ -129,14 +133,26 @@ private int decompressDict(final ZChunkFile zChunkFile) { final int copied = IOUtil.copy(decompressedDictStream, fileOutputStream); } catch (final FileNotFoundException fnfe) { + cleanPartialFile(target); throw new UncompressException("Unable to create parent dir or file: [" + target.getAbsolutePath() + "].", fnfe); } catch (final IOException ex) { + cleanPartialFile(target); throw new UncompressException("Unable to write file: [" + target.getAbsolutePath() + "].", ex); } return 0; } + private void cleanPartialFile(final File target) { + if (target.exists()) { + try { + Files.delete(target.toPath()); + } catch (final IOException ioEx) { + LOG.log(Level.WARNING, ioEx, () -> "unable to delete file [" + target.getAbsolutePath() + "]."); + } + } + } + private File getTargetFile() { if (null != this.outputFile) { return this.outputFile;