Class FileUtils


  • public final class FileUtils
    extends Object
    • Method Detail

      • copy

        public static void copy​(File src,
                                File dst)
                         throws IOException
        Copies source to destination. If source and destination are the same, does nothing. Both single files and directories are handled.
        Parameters:
        src - the source file or directory
        dst - the destination file or directory
        Throws:
        IOException
      • copyFile

        @Deprecated
        public static void copyFile​(File src,
                                    File dst)
                             throws IOException
        Deprecated.
        since 10.1 - use FileUtils.copyFile(File, File) or FileUtils.copyFileToDirectory(File, File) instead.
        Throws:
        IOException
      • copyTree

        @Deprecated
        public static void copyTree​(File src,
                                    File dst)
                             throws IOException
        Deprecated.
        since 10.1 - waiting ReloadComponent to be cleaned
        Copies recursively source to destination.

        The source file is assumed to be a directory.

        Parameters:
        src - the source directory
        dst - the destination directory
        Throws:
        IOException
      • getFilePathFromUrl

        public static String getFilePathFromUrl​(URL url)
        Decodes an URL path so that is can be processed as a filename later.
        Parameters:
        url - the Url to be processed.
        Returns:
        the decoded path.
      • getFileFromURL

        public static File getFileFromURL​(URL url)
      • getParentPath

        public static String getParentPath​(String path)
      • getFileName

        public static String getFileName​(String path)
      • getFileExtension

        public static String getFileExtension​(String path)
      • getFileNameNoExt

        public static String getFileNameNoExt​(String path)
      • getResourcePathFromContext

        public static String getResourcePathFromContext​(String resource)
        Retrieves the total path of a resource from the Thread Context.
        Parameters:
        resource - the resource name to be retrieved.
        Returns:
        the decoded path or null if the resource doesn't exist.
      • getResourceFileFromContext

        public static File getResourceFileFromContext​(String resource)
      • findFiles

        public static File[] findFiles​(File root,
                                       String pattern,
                                       boolean recurse)
      • collectFiles

        public static void collectFiles​(File root,
                                        List<File> result)
      • urlToFile

        public static File urlToFile​(String url)
                              throws MalformedURLException
        Create a file handler (this doesn't create a real file) given a file URI. This method can be used to create files from invalid URL strings (e.g. containing spaces ..)
        Returns:
        a file object
        Throws:
        MalformedURLException
      • urlToFile

        public static File urlToFile​(URL url)
      • areFilesContentEquals

        public static boolean areFilesContentEquals​(String expected,
                                                    String source)
        Compares two files content as String even if their EOL are different
        Parameters:
        expected - a file content with Windows or Unix like EOL
        source - another file content with Windows or Unix like EOL
        Returns:
        the result of equals after replacing their EOL
      • getSafeFilename

        public static String getSafeFilename​(String filename)
        Returns a safe filename, replacing unsafe characters (: \ / * ..) with "_". For instance, it turns "tmp/../2349:876398/foo.png" into "tmp___2349_876398_foo.png"
        Parameters:
        filename - the filename
        Returns:
        the safe filename with underscores instead of unsafe characters
        Since:
        9.1
      • checkPathTraversal

        public static void checkPathTraversal​(String path)
        Checks if a path contains unwanted characters leading to a path traversal.
        Since:
        2021.40
      • getZipEntryAsFile

        public static File getZipEntryAsFile​(File parent,
                                             String childPath)
        Returns the File targeted by a path given a specific parent File.
        Since:
        2021.40
      • checkZipSlip

        public static void checkZipSlip​(File parent,
                                        File child)
        Checks if a path is contained in another. Throws an IllegalArgumentException otherwise.
        Since:
        2021.40