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