View Javadoc

1   package com.atlassian.plugin.util;
2   
3   import com.atlassian.plugin.util.zip.UrlUnzipper;
4   
5   import java.io.File;
6   import java.io.IOException;
7   import java.net.URISyntaxException;
8   import java.net.URL;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  public class FileUtils
14  {
15      private static final Logger log = LoggerFactory.getLogger(FileUtils.class);
16  
17      /**
18       * Extract the zip from the URL into the destination directory, but only if the contents haven't already been
19       * unzipped.  If the directory contains different contents than the zip, the directory is cleaned out
20       * and the files are unzipped.
21       *
22       * @param zipUrl The zip url
23       * @param destDir The destination directory for the zip contents
24       */
25      public static void conditionallyExtractZipFile(URL zipUrl, File destDir)
26      {
27          try
28          {
29              UrlUnzipper unzipper = new UrlUnzipper(zipUrl, destDir);
30              unzipper.conditionalUnzip();
31          }
32          catch (IOException e)
33          {
34              log.error("Found " + zipUrl + ", but failed to read file", e);
35          }
36      }
37  
38      /**
39       * If possible, return the File equivalent of a URL.
40       * @return the file, or null if it does not represent a file
41       */
42      public static File toFile(final URL url)
43      {
44          if (!"file".equalsIgnoreCase(url.getProtocol())) {
45              return null;
46          }
47  
48          return org.apache.commons.io.FileUtils.toFile(url);
49      }
50  
51      /**
52       * @deprecated Since 2.0.0
53       */
54      public static void deleteDir(File directory)
55      {
56          try
57          {
58              org.apache.commons.io.FileUtils.deleteDirectory(directory);
59          } catch (IOException e)
60          {
61              log.error("Unable to delete directory: "+directory.getAbsolutePath(), e);
62          }
63      }
64  
65  }