Package org.nuxeo.common.utils
Class FileUtils
java.lang.Object
org.nuxeo.common.utils.FileUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 voidstatic voidCopies source to destination.protected static voidcopyTree(int rootIndex, File src, File dst, Path prefix, PathFilter filter) static voidcopyTree(File src, File dst, Path prefix, PathFilter filter) static File[]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 FileCreate 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
-
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
-