Package org.nuxeo.common.utils
Class FileUtils
- java.lang.Object
-
- org.nuxeo.common.utils.FileUtils
-
public final class FileUtils extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static booleanareFilesContentEquals(String expected, String source)Compares two files content as String even if their EOL are differentstatic voidcheckPathTraversal(String path)Checks if a path contains unwanted characters leading to a path traversal.static voidcheckZipSlip(File parent, File child)Checks if a path is contained in another.static voidcollectFiles(File root, List<File> result)static voidcollectFiles(File root, FileNamePattern pattern, List<File> result)static voidcopy(File[] src, File dst)static voidcopy(File src, File dst)Copies source to destination.static voidcopyFile(File src, File dst)Deprecated.since 10.1 - useFileUtils.copyFile(File, File)orFileUtils.copyFileToDirectory(File, File)instead.protected static voidcopyTree(int rootIndex, File src, File dst, Path prefix, PathFilter filter)static voidcopyTree(File src, File dst)Deprecated.since 10.1 - waiting ReloadComponent to be cleanedstatic voidcopyTree(File src, File dst, PathFilter filter)Deprecated.since 10.1 - seems unusedstatic voidcopyTree(File src, File dst, Path prefix, PathFilter filter)static File[]findFiles(File root, String pattern, boolean recurse)static StringgetFileExtension(String path)static FilegetFileFromURL(URL url)static StringgetFileName(String path)static StringgetFileNameNoExt(String path)static StringgetFilePathFromUrl(URL url)Decodes an URL path so that is can be processed as a filename later.static StringgetParentPath(String path)static FilegetResourceFileFromContext(String resource)static StringgetResourcePathFromContext(String resource)Retrieves the total path of a resource from the Thread Context.static StringgetSafeFilename(String filename)Returns a safe filename, replacing unsafe characters (: \ / * ..) with "_".static FilegetZipEntryAsFile(File parent, String childPath)Returns the File targeted by a path given a specific parent File.static FileurlToFile(String url)Create a file handler (this doesn't create a real file) given a file URI.static FileurlToFile(URL url)
-
-
-
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 directorydst- the destination file or directory- Throws:
IOException
-
copy
public static void copy(File[] src, File dst) throws IOException
- Throws:
IOException
-
copyFile
@Deprecated public static void copyFile(File src, File dst) throws IOException
Deprecated.since 10.1 - useFileUtils.copyFile(File, File)orFileUtils.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 cleanedCopies recursively source to destination.The source file is assumed to be a directory.
- Parameters:
src- the source directorydst- the destination directory- Throws:
IOException
-
copyTree
@Deprecated public static void copyTree(File src, File dst, PathFilter filter) throws IOException
Deprecated.since 10.1 - seems unused- Throws:
IOException
-
copyTree
public static void copyTree(File src, File dst, Path prefix, PathFilter filter) throws IOException
- Throws:
IOException
-
copyTree
protected static void copyTree(int rootIndex, File src, File dst, Path prefix, PathFilter filter) throws IOException- 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.
-
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.
-
collectFiles
public static void collectFiles(File root, FileNamePattern pattern, 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
-
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 EOLsource- 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
-
-