Skip to content

Commit

Permalink
Delete partial file if exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarwell committed Jun 1, 2019
1 parent a76119b commit c7294aa
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/io/github/zchunk/app/commands/Unzck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c7294aa

Please # to comment.