# HG changeset patch # User Sascha L. Teichmann # Date 1349526040 -7200 # Node ID e39e23320a23b8c236ba2db66ae7bf4f5bed7f36 # Parent 403a6793077ceb029d3a4840acf525f2bd3e5eb6 Fixed small problem with extractiong zip files. diff -r 403a6793077c -r e39e23320a23 ChangeLog --- a/ChangeLog Wed Oct 03 16:32:33 2012 +0200 +++ b/ChangeLog Sat Oct 06 14:20:40 2012 +0200 @@ -1,3 +1,9 @@ +2012-10-06 Sascha L. Teichmann + + * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java: + Made extracting of zip archives more robust, lose file handles in case + of error and fix a bug when creating sub dirs. + 2012-09-30 Björn Ricks * pom.xml: Comment out build number plugin diff -r 403a6793077c -r e39e23320a23 artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java --- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Wed Oct 03 16:32:33 2012 +0200 +++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Sat Oct 06 14:20:40 2012 +0200 @@ -358,37 +358,48 @@ } ZipFile zipFile = new ZipFile(archive); - Enumeration entries = zipFile.entries(); - - byte[] buffer = new byte[16384]; - int len; - while (entries.hasMoreElements()) { - ZipEntry entry = (ZipEntry) entries.nextElement(); - - String entryFileName = entry.getName(); + try { + Enumeration entries = zipFile.entries(); - File dir = dir = buildDirectoryHierarchyFor(entryFileName, destDir); - if (!dir.exists()) { - dir.mkdirs(); - } + byte [] buffer = new byte[16384]; - if (!entry.isDirectory()) { - BufferedOutputStream bos = new BufferedOutputStream( - new FileOutputStream(new File(destDir, entryFileName))); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); - BufferedInputStream bis = new BufferedInputStream(zipFile - .getInputStream(entry)); + String entryFileName = entry.getName(); - while ((len = bis.read(buffer)) > 0) { - bos.write(buffer, 0, len); + File dir = buildDirectoryHierarchyFor(entryFileName, destDir); + if (!dir.exists()) { + dir.mkdirs(); } - bos.flush(); - bos.close(); - bis.close(); + if (!entry.isDirectory()) { + BufferedInputStream bis = new BufferedInputStream( + zipFile.getInputStream(entry)); + try { + BufferedOutputStream bos = new BufferedOutputStream( + new FileOutputStream(new File(destDir, entryFileName))); + + try { + int len; + while ((len = bis.read(buffer)) > 0) { + bos.write(buffer, 0, len); + } + bos.flush(); + } + finally { + bos.close(); + } + } + finally { + bis.close(); + } + } // is file } } - zipFile.close(); + finally { + zipFile.close(); + } } private static File buildDirectoryHierarchyFor( @@ -396,7 +407,6 @@ File destDir) { int lastIndex = entryName.lastIndexOf('/'); - String entryFileName = entryName.substring(lastIndex + 1); String internalPathToEntry = entryName.substring(0, lastIndex + 1); return new File(destDir, internalPathToEntry); }