Version 4.4.5
[nikiroo-utils.git] / src / be / nikiroo / utils / IOUtils.java
index bf0686babca9e7addfbbd810a7a8e4d5fd66756d..e9a378c0fd6e279c1568afb848808ab83f9cdf5e 100644 (file)
@@ -13,10 +13,11 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
 /**
- * This class offer some utilities based around Streams.
+ * This class offer some utilities based around Streams and Files.
  * 
  * @author niki
  */
@@ -89,8 +90,12 @@ public class IOUtils {
                                }
                                zip.putNextEntry(new ZipEntry(base + "/"));
                        }
-                       for (File file : target.listFiles()) {
-                               zip(zip, base, file, false);
+
+                       File[] files = target.listFiles();
+                       if (files != null) {
+                               for (File file : files) {
+                                       zip(zip, base, file, false);
+                               }
                        }
                } else {
                        if (base == null || base.isEmpty()) {
@@ -137,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}.
         * 
@@ -156,7 +213,23 @@ public class IOUtils {
                        dir.mkdirs();
                }
 
-               FileWriter writerVersion = new FileWriter(new File(dir, filename));
+               writeSmallFile(new File(dir, filename), content);
+       }
+
+       /**
+        * Write the {@link String} content to {@link File}.
+        * 
+        * @param file
+        *            the {@link File} to write
+        * @param content
+        *            the content
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public static void writeSmallFile(File file, String content)
+                       throws IOException {
+               FileWriter writerVersion = new FileWriter(file);
                try {
                        writerVersion.write(content);
                } finally {
@@ -234,13 +307,13 @@ public class IOUtils {
                        throws IOException {
                List<File> list = deltree(target, null);
                if (exception && !list.isEmpty()) {
-                       String slist = "";
+                       StringBuilder slist = new StringBuilder();
                        for (File file : list) {
-                               slist += "\n" + file.getPath();
+                               slist.append("\n").append(file.getPath());
                        }
 
                        throw new IOException("Cannot delete all the files from: <" //
-                                       + target + ">:" + slist);
+                                       + target + ">:" + slist.toString());
                }
 
                return list.isEmpty();
@@ -375,4 +448,24 @@ public class IOUtils {
 
                return array;
        }
+
+       /**
+        * Convert the {@link File} into a byte array.
+        * 
+        * @param file
+        *            the input {@link File}
+        * 
+        * @return the array
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public static byte[] toByteArray(File file) throws IOException {
+               FileInputStream fis = new FileInputStream(file);
+               try {
+                       return toByteArray(fis);
+               } finally {
+                       fis.close();
+               }
+       }
 }