Package org.apache.ofbiz.base.util
Class FileUtil
java.lang.Object
org.apache.ofbiz.base.util.FileUtil
File Utilities
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
containsString
(Reader reader, String searchString) Search for the specifiedsearchString
in the givenReader
.static boolean
containsString
(String fileName, String searchString) Search for the specifiedsearchString
in the given filename.static File
createFileWithNormalizedPath
(String filePath) Creates a File with a normalized file path This useful to prevent path traversal security issues cf.findXmlFiles
(String basePath, String partialPath, String rootElementName, String xsdOrDtdName) static OutputStream
getBufferedOutputStream
(String path, String name) static Writer
getBufferedWriter
(String path, String name) static File
static File
static String
getPatchedFileName
(String path, String fileName) static boolean
Check if the specifiedfileName
exists and is a file (not a directory) If the specified file doesn't exist or is a directoryFALSE
returns.static String
localizePath
(String path) Converts a file path to one that is compatible with the host operating system.static File
This useful to prevents Zip slip vulnerability cf.static String
normalizeFilePath
(String filePath) Normalizes a file path This useful to prevent path traversal security issuesstatic String
readString
(String encoding, File inFile) static StringBuffer
readTextFile
(File file, boolean newline) static StringBuffer
readTextFile
(String fileName, boolean newline) static void
searchFiles
(List<File> fileList, File path, FilenameFilter filter, boolean includeSubfolders) static boolean
Unzip file structure of the given zipFile to specified outputFolder The Zip slip vulnerability is handled since version 1.3.3 of net.lingala.zip4j.ZipFile; unzipFileToFolder is not as reliable and does not handle passwordsstatic boolean
unzipFileToFolder
(File zipFile, String outputFolder, boolean handleZipSlip) Unzip file structure of the given zipFile to specified outputFolderstatic void
writeString
(String fileName, String s) static void
writeString
(String encoding, String s, File outFile) static void
writeString
(String path, String name, String s) static void
writeString
(String path, String name, String encoding, String s) Writes a file from a string with a specified encoding.static ByteArrayInputStream
zipFileStream
(InputStream fileStream, String fileName) For an inputStream and a file name, create a zip stream containing only one entry with the inputStream set to fileName If fileName is empty, generate a unique one.
-
Method Details
-
getFile
-
getFile
-
localizePath
Converts a file path to one that is compatible with the host operating system.- Parameters:
path
- The file path to convert.- Returns:
- The converted file path.
-
writeString
- Throws:
IOException
-
writeString
-
writeString
public static void writeString(String path, String name, String encoding, String s) throws IOException Writes a file from a string with a specified encoding.- Parameters:
path
-name
-encoding
-s
-- Throws:
IOException
-
writeString
- Throws:
IOException
-
getBufferedWriter
- Throws:
IOException
-
getBufferedOutputStream
- Throws:
IOException
-
getPatchedFileName
- Throws:
IOException
-
readTextFile
public static StringBuffer readTextFile(File file, boolean newline) throws FileNotFoundException, IOException - Throws:
FileNotFoundException
IOException
-
readTextFile
public static StringBuffer readTextFile(String fileName, boolean newline) throws FileNotFoundException, IOException - Throws:
FileNotFoundException
IOException
-
readString
- Throws:
IOException
-
searchFiles
public static void searchFiles(List<File> fileList, File path, FilenameFilter filter, boolean includeSubfolders) throws IOException - Throws:
IOException
-
findFiles
public static List<File> findFiles(String fileExt, String basePath, String partialPath, String stringToFind) throws IOException - Throws:
IOException
-
findXmlFiles
public static List<File> findXmlFiles(String basePath, String partialPath, String rootElementName, String xsdOrDtdName) throws IOException - Throws:
IOException
-
containsString
Search for the specifiedsearchString
in the givenReader
.- Parameters:
reader
- A Reader in which the String will be searched.searchString
- The String to search for- Returns:
TRUE
if thesearchString
is found;FALSE
otherwise.- Throws:
IOException
-
containsString
Search for the specifiedsearchString
in the given filename. If the specified file doesn't exist,FALSE
returns.- Parameters:
fileName
- A full path to a file in which the String will be searched.searchString
- The String to search for- Returns:
TRUE
if thesearchString
is found;FALSE
otherwise.- Throws:
IOException
-
isFile
Check if the specifiedfileName
exists and is a file (not a directory) If the specified file doesn't exist or is a directoryFALSE
returns.- Parameters:
fileName
- A full path to a file in which the String will be searched.- Returns:
TRUE
if thefileName
exists and is a file (not a directory)FALSE
otherwise.
-
zipFileStream
public static ByteArrayInputStream zipFileStream(InputStream fileStream, String fileName) throws IOException For an inputStream and a file name, create a zip stream containing only one entry with the inputStream set to fileName If fileName is empty, generate a unique one.- Parameters:
fileStream
-fileName
-- Returns:
- zipStream
- Throws:
IOException
-
newFile
This useful to prevents Zip slip vulnerability cf. https://snyk.io/research/zip-slip-vulnerability- Parameters:
destinationDirName
- The file path name to normalizezipEntry
- Zip entry to check before creating- Returns:
- A file in destinationDir
- Throws:
IOException
-
unzipFileToFolder
public static boolean unzipFileToFolder(File zipFile, String outputFolder, boolean handleZipSlip) throws IOException Unzip file structure of the given zipFile to specified outputFolder- Parameters:
zipFile
-outputFolder
-handleZipSlip
- if true FileUtil::newFile is used- Throws:
IOException
-
createFileWithNormalizedPath
Creates a File with a normalized file path This useful to prevent path traversal security issues cf. OFBIZ-9973 for more details- Parameters:
filePath
- The file path to normalize- Returns:
- A File with a normalized file path
-
normalizeFilePath
Normalizes a file path This useful to prevent path traversal security issues- Parameters:
filePath
- The file path to normalize- Returns:
- A normalized file path
-
unZip
Unzip file structure of the given zipFile to specified outputFolder The Zip slip vulnerability is handled since version 1.3.3 of net.lingala.zip4j.ZipFile; unzipFileToFolder is not as reliable and does not handle passwords- Parameters:
source
-destination
-password
- optional- Returns:
- true if OK
-