From b977b55da647e2c09596065ab1e2b6c7dd2820ef Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 13 May 2005 08:09:22 +0000 Subject: [PATCH] and could leave streams open. PR 34893 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278254 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 +++ src/main/org/apache/tools/ant/taskdefs/Untar.java | 10 +++++++--- src/main/org/apache/tools/zip/ZipFile.java | 13 +++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 82fd1dc36..b898513eb 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -213,6 +213,9 @@ Fixed bugs: * Granularity attribute for task was undocumented. Bugzilla report 34871. +* and could leave file handles open on invalid + archives. Bugzilla report 34893. + Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java index 059881e6c..39c758579 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Untar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java @@ -90,13 +90,13 @@ public class Untar extends Expand { * @see Expand#expandFile(FileUtils, File, File) */ protected void expandFile(FileUtils fileUtils, File srcF, File dir) { + FileInputStream fis = null; TarInputStream tis = null; try { log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); + fis = new FileInputStream(srcF); tis = new TarInputStream( - compression.decompress(srcF, - new BufferedInputStream( - new FileInputStream(srcF)))); + compression.decompress(srcF, new BufferedInputStream(fis))); TarEntry te = null; FileNameMapper mapper = getMapper(); while ((te = tis.getNextEntry()) != null) { @@ -111,6 +111,10 @@ public class Untar extends Expand { ioe, getLocation()); } finally { FileUtils.close(tis); + if (tis == null) { + FileUtils.close(fis); + } + } } diff --git a/src/main/org/apache/tools/zip/ZipFile.java b/src/main/org/apache/tools/zip/ZipFile.java index b55132deb..cf3638a54 100644 --- a/src/main/org/apache/tools/zip/ZipFile.java +++ b/src/main/org/apache/tools/zip/ZipFile.java @@ -138,8 +138,17 @@ public class ZipFile { public ZipFile(File f, String encoding) throws IOException { this.encoding = encoding; archive = new RandomAccessFile(f, "r"); - populateFromCentralDirectory(); - resolveLocalFileHeaderData(); + try { + populateFromCentralDirectory(); + resolveLocalFileHeaderData(); + } catch (IOException e) { + try { + archive.close(); + } catch (IOException e2) { + // swallow, throw the original exception instead + } + throw e; + } } /**