From 97e84a50c9ecc8db8795046dfabe33f736950424 Mon Sep 17 00:00:00 2001 From: "Simon.Nagl" Date: Fri, 22 Nov 2019 10:59:42 +0100 Subject: [PATCH] Always close SevenZFile after extraction. Fixes #4 --- .../eu/emundo/gradle/sevenz/UnSevenZ.groovy | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/groovy/eu/emundo/gradle/sevenz/UnSevenZ.groovy b/src/main/groovy/eu/emundo/gradle/sevenz/UnSevenZ.groovy index 3cb657a..fedac46 100644 --- a/src/main/groovy/eu/emundo/gradle/sevenz/UnSevenZ.groovy +++ b/src/main/groovy/eu/emundo/gradle/sevenz/UnSevenZ.groovy @@ -21,25 +21,27 @@ class UnSevenZ extends DefaultTask { @TaskAction void extract(IncrementalTaskInputs inputs) { - SevenZFile sevenZFile = new SevenZFile(sourceFile) - SevenZArchiveEntry entry - while ((entry = sevenZFile.getNextEntry()) != null) { - if (entry.isDirectory()) { - continue - } - File curfile = new File(outputDir, entry.getName()) - File parent = curfile.getParentFile() - if (parent != null && !parent.exists()) { - parent.mkdirs() - } + new SevenZFile(sourceFile).withCloseable {sevenZFile -> + SevenZArchiveEntry entry + while ((entry = sevenZFile.getNextEntry()) != null) { + if (entry.isDirectory()) { + continue + } + File curfile = new File(outputDir, entry.getName()) + File parent = curfile.getParentFile() + if (parent != null && !parent.exists()) { + parent.mkdirs() + } - int currByte = 0; - new BufferedOutputStream(new FileOutputStream(curfile)).withStream { ostream -> - while( (currByte = sevenZFile.read()) != -1 ) { - ostream.write(currByte); + int currByte = 0; + new BufferedOutputStream(new FileOutputStream(curfile)).withStream { ostream -> + while( (currByte = sevenZFile.read()) != -1 ) { + ostream.write(currByte); + } } } } + } }