changeset 435:e39e23320a23

Fixed small problem with extractiong zip files.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 06 Oct 2012 14:20:40 +0200
parents 403a6793077c
children 15179c77aa1d
files ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java
diffstat 2 files changed, 40 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- 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	<sascha.teichmann@intevation.de>
+
+	* 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	<bjoern.ricks@intevation.de>
 
 	* pom.xml: Comment out build number plugin
--- 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<? extends ZipEntry> 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);
     }

http://dive4elements.wald.intevation.org