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