New: IOUtils.unzip()
authorNiki Roo <niki@nikiroo.be>
Fri, 23 Mar 2018 13:49:03 +0000 (14:49 +0100)
committerNiki Roo <niki@nikiroo.be>
Fri, 23 Mar 2018 13:49:03 +0000 (14:49 +0100)
changelog.md
src/be/nikiroo/utils/IOUtils.java

index 7b44b8ba723a391eb3c00819a83c4849b046b2ae..8892800aa3bd14580c7460839029a4d4f7d8a1b4 100644 (file)
@@ -1,5 +1,9 @@
 # nikiroo-utils
 
 # nikiroo-utils
 
+## Version 4.3.0
+
+- New: IOUtils.Unzip()
+
 ## Version 4.2.1
 
 - Fix small bug in Downloader
 ## Version 4.2.1
 
 - Fix small bug in Downloader
index d83311449c3bb5016ae35b396284f56822786ee0..3e2f6e761481fbf7c0e741539c7f811dbf272daa 100644 (file)
@@ -13,6 +13,7 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
 /**
 import java.util.zip.ZipOutputStream;
 
 /**
@@ -141,6 +142,58 @@ public class IOUtils {
                }
        }
 
                }
        }
 
+       /**
+        * Unzip the given ZIP file into the target directory.
+        * 
+        * @param zipFile
+        *            the ZIP file
+        * @param targetDirectory
+        *            the target directory
+        * 
+        * @return the number of extracted files (not directories)
+        * 
+        * @throws IOException
+        *             in case of I/O errors
+        */
+       public static long unzip(File zipFile, File targetDirectory)
+                       throws IOException {
+               long count = 0;
+
+               if (targetDirectory.exists() && targetDirectory.isFile()) {
+                       throw new IOException("Cannot unzip " + zipFile + " into "
+                                       + targetDirectory + ": it is not a directory");
+               }
+
+               targetDirectory.mkdir();
+               if (!targetDirectory.exists()) {
+                       throw new IOException("Cannot create target directory "
+                                       + targetDirectory);
+               }
+
+               FileInputStream in = new FileInputStream(zipFile);
+               try {
+                       ZipInputStream zipStream = new ZipInputStream(in);
+                       try {
+                               for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream
+                                               .getNextEntry()) {
+                                       File file = new File(targetDirectory, entry.getName());
+                                       if (entry.isDirectory()) {
+                                               file.mkdirs();
+                                       } else {
+                                               IOUtils.write(zipStream, file);
+                                               count++;
+                                       }
+                               }
+                       } finally {
+                               zipStream.close();
+                       }
+               } finally {
+                       in.close();
+               }
+
+               return count;
+       }
+
        /**
         * Write the {@link String} content to {@link File}.
         * 
        /**
         * Write the {@link String} content to {@link File}.
         *