Package org.nuxeo.common.utils
Class FileUtils
java.lang.Object
org.nuxeo.common.utils.FileUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
areFilesContentEquals
(String expected, String source) Compares two files content as String even if their EOL are differentstatic void
checkPathTraversal
(String path) Checks if a path contains unwanted characters leading to a path traversal.static void
checkZipSlip
(File parent, File child) Checks if a path is contained in another.static void
collectFiles
(File root, List<File> result) static void
collectFiles
(File root, FileNamePattern pattern, List<File> result) static void
static void
Copies source to destination.static void
Deprecated.protected static void
copyTree
(int rootIndex, File src, File dst, Path prefix, PathFilter filter) static void
Deprecated.since 10.1 - waiting ReloadComponent to be cleanedstatic void
copyTree
(File src, File dst, PathFilter filter) Deprecated.since 10.1 - seems unusedstatic void
copyTree
(File src, File dst, Path prefix, PathFilter filter) static File[]
static String
getFileExtension
(String path) static File
getFileFromURL
(URL url) static String
getFileName
(String path) static String
getFileNameNoExt
(String path) static String
getFilePathFromUrl
(URL url) Decodes an URL path so that is can be processed as a filename later.static String
getParentPath
(String path) static File
getResourceFileFromContext
(String resource) static String
getResourcePathFromContext
(String resource) Retrieves the total path of a resource from the Thread Context.static String
getSafeFilename
(String filename) Returns a safe filename, replacing unsafe characters (: \ / * ..) with "_".static File
getZipEntryAsFile
(File parent, String childPath) Returns the File targeted by a path given a specific parent File.static File
Create a file handler (this doesn't create a real file) given a file URI.static File
-
Method Details
-
copy
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
- Throws:
IOException
-
copyFile
Deprecated.since 10.1 - useFileUtils.copyFile(File, File)
orFileUtils.copyFileToDirectory(File, File)
instead.- Throws:
IOException
-
copyTree
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.since 10.1 - seems unused- Throws:
IOException
-
copyTree
- Throws:
IOException
-
copyTree
protected static void copyTree(int rootIndex, File src, File dst, Path prefix, PathFilter filter) throws IOException - Throws:
IOException
-
getFilePathFromUrl
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
-
getParentPath
-
getFileName
-
getFileExtension
-
getFileNameNoExt
-
getResourcePathFromContext
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
-
findFiles
-
collectFiles
-
collectFiles
-
urlToFile
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
-
areFilesContentEquals
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
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
Checks if a path contains unwanted characters leading to a path traversal.- Since:
- 2021.40
-
getZipEntryAsFile
Returns the File targeted by a path given a specific parent File.- Since:
- 2021.40
-
checkZipSlip
Checks if a path is contained in another. Throws an IllegalArgumentException otherwise.- Since:
- 2021.40
-
FileUtils.copyFile(File, File)
orFileUtils.copyFileToDirectory(File, File)
instead.