Package flexagon.ff.common.core.utils
Class FlexFileUtils
java.lang.Object
flexagon.ff.common.core.utils.FlexFileBase
flexagon.ff.common.core.utils.FlexFileUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcalculateDirectoryHash(File pDirToHash) Calculate directory hash by considering all files in the folder.static StringcalculateFileHash(File pFile) Calculates hash based on file content.static StringconvertBase64EncodedString(String pEncodedString) static voidconvertBase64EncodedStringToFile(String pEncodedString, File pFile) Deprecated.Use java 8 Base64 when java 6 dependency is removed.static StringconvertByteArrayToHexString(byte[] arrayBytes) static StringconvertFileToBase64String(File pFile) static StringconvertFileToBase64String(Path pFile) static StringconvertGlobToRegEx(String line) static voidcopyDirectory(File srcDir, File destDir) Contents of SrcDir are copied to DestDir.static voidCopy file.static voidstatic voidDelete file or directory represented by supplied file objectstatic voidDelete file or directory represented by supplied file objectstatic voiddeleteDirectory(File directory) Delete directory including all of its contents.static voiddeleteDirectory(Path directory) Delete directory including all of its contents.static voiddeleteDirectorySafely(File pDirectory) Delete a directory safely.static voiddeleteDirectorySafely(Path pDirectory) Delete a directory safely.static voiddeleteFile(File file) Delete filestatic voiddeleteFile(Path file) Delete filestatic voiddeleteFileSafely(File file) Delete file, ignoring if delete failsstatic voiddeleteFileSafely(Path file) Delete file, ignoring if delete failsstatic voiddeleteSafely(File pFileOrDirectory) Delete File or Directory safely.static voiddeleteSafely(Path pFileOrDirectory) Delete File or Directory safely.static voidensureDirectory(File directory) static voidensureDirectory(File directory, boolean log) static voidensureDirectory(Path directory) static StringgetExtensionFromFileName(String pFileNamePath) static StringgetExtensionFromFileName(String pFileNamePath, String pDefaultExtension) static Collection<File>getListOfFilesMatchingCaseInsensitiveSuffix(File sourceFolder, String suffix) Finds files that end with suffix.static File[]getListOfFilesMatchingExt(File sourceFolder, String pExtension) Don't put the .static String[]getListOfFilesMatchingExt(String pDirectory, String pExtension) Returns an empty list if no files are found.static Collection<File>getListOfFilesMatchingRegex(File sourceFolder, String regex) static Collection<File>getListOfFilesMatchingSuffix(File sourceFolder, String suffix) Finds files that end with suffix.static Collection<File>getListOfFilesMatchingWildcard(File sourceFolder, String wildCardExpression) static StringgetQuotedFilePath(File pFile) Returns a properly quoted file path, depending on the operating system.static StringgetSafeFileName(String pFileName) Returns a filename that is safe for use on all operating systems.static StringgetSafeFileName(String pFileName, String pReplacement) Returns a filename that is safe for use on all operating systems.static StringhashInputStream(InputStream inputStream, String pFileName) static String[]Consider using Files.walk instead.static File[]Consider using Files.walk instead.static File[]listFilesNewerThan(File directory, long timestamp) static File[]listFilesOlderThan(File directory, long timestamp) static StringlistStringToString(List<String> lines) static StringprobeContentType(File pFile) static StringprobeContentType(String pFileName) in Java 11+, need to include javax.activation as a dependency to use this method.static StringprobeContentType(Path pFile) Reads a File into a List<String>read(InputStream is) Reads an input stream into a List<String>This method will not work if the file has 2 or more = signs on a line.static byte[]readBytesFromFile(File file) Consider streaming.static Stringstatic Stringstatic StringreadFileAsString(File file) static Stringstatic voidstatic voidstatic voidunZip(InputStream pInputStream, File pOutputFolder) static voidunZip(InputStream pInputStream, Path pOutputFolder) static voidstatic voidstatic voidWrite a list of strings to a file with UTF-8 charsetstatic voidWrite list of strings to a file with the specified charsetstatic voidwriteAsMap(File file, Map<String, String> map) static voidwriteBytesToFile(File file, byte[] pData) Writes a byte array to a file Throws FlexRuntimeExceptionstatic voidwriteInputStreamToFile(File resultsFile, InputStream inputStream) Writes an input stream to a file Throws FlexRuntimeExceptionstatic voidwriteInputStreamToFile(Path resultsFile, InputStream inputStream) Writes an input stream to a file Throws FlexRuntimeExceptionstatic intstatic voidzipDirectory(String pNamePrefix, File pDirectory, ZipOutputStream pZos) Add a directory to a ZipOutputStream.static voidzipFile(String pNamePrefix, File pFile, ZipOutputStream pZipOutputStream) Add a file into a zip output streamstatic InputStreamzipInputStreamsToInputStream(Map<String, InputStream> pInputStreams) Combines multiple input streams into a single compressed input streamstatic ByteArrayOutputStreamzipInputStreamsToOutputStream(Map<String, InputStream> pInputStreams) Combines multiple input streams into a single zip streamMethods inherited from class flexagon.ff.common.core.utils.FlexFileBase
isSymbolicLink, isSymbolicLink, setOwnerOnlyFilePermissions
-
Method Details
-
ensureDirectory
-
ensureDirectory
-
ensureDirectory
-
deleteDirectory
Delete directory including all of its contents.- Parameters:
directory-
-
deleteDirectory
Delete directory including all of its contents.- Parameters:
directory-
-
deleteDirectorySafely
Delete a directory safely. Meaning it will not throw an exception if it does not complete correctly.- Parameters:
pDirectory-
-
deleteDirectorySafely
Delete a directory safely. Meaning it will not throw an exception if it does not complete correctly.- Parameters:
pDirectory-
-
deleteFile
Delete file- Parameters:
file-
-
deleteFile
Delete file- Parameters:
file-
-
deleteFileSafely
Delete file, ignoring if delete fails- Parameters:
file-
-
deleteFileSafely
Delete file, ignoring if delete fails- Parameters:
file-
-
delete
Delete file or directory represented by supplied file object- Parameters:
fileOrDirectory-
-
delete
Delete file or directory represented by supplied file object- Parameters:
fileOrDirectory-
-
deleteSafely
Delete File or Directory safely. Meaning no exception will be thrown if it fails.- Parameters:
pFileOrDirectory-
-
deleteSafely
Delete File or Directory safely. Meaning no exception will be thrown if it fails.- Parameters:
pFileOrDirectory-
-
listFiles
Consider using Files.walk instead. checks for access issues, if null is returned from listFiles, it means that this process can not access that folder.- Parameters:
directory-- Returns:
-
listFilesOlderThan
-
listFilesNewerThan
-
list
Consider using Files.walk instead. checks for access issues, if null is returned from list, it means that this process can not access that folder.- Parameters:
directory-- Returns:
-
copyFile
-
copyFile
Copy file.- Parameters:
srcFile-destFile-
-
copyDirectory
Contents of SrcDir are copied to DestDir. Destination directory must be empty. Source Directory must exist and must represent Directory.- Parameters:
srcDir-destDir-
-
rename
-
readBytesFromFile
Consider streaming. Do you really want the whole byte[] in memory? If you are unzipping or something don't do this...- Parameters:
file-- Returns:
-
writeInputStreamToFile
Writes an input stream to a file Throws FlexRuntimeException- Parameters:
resultsFile- - file to writeinputStream- - InputStream to write. The inputStream is not closed, and should be by the caller.
-
writeInputStreamToFile
Writes an input stream to a file Throws FlexRuntimeException- Parameters:
resultsFile- - file to writeinputStream- - InputStream to write. The inputStream is not closed, and should be by the caller.
-
writeBytesToFile
Writes a byte array to a file Throws FlexRuntimeException- Parameters:
file- - file to writepData- - data for file.
-
read
Reads a File into a List<String>- Parameters:
file- - file to read.- Returns:
- the data from the file.
-
read
Reads an input stream into a List<String>- Parameters:
is- - input stream- Returns:
- the data from the input stream.
-
readFileAsString
-
readFileAsString
-
readFileAsMultiLineString
-
readFileAsMultiLineString
-
listStringToString
-
readAsMap
This method will not work if the file has 2 or more = signs on a line. It should probably either not be used - use Properties File classes from Java instead, or it should be rewritten with using indexOf(=) instead of split("=").- Parameters:
file-- Returns:
-
write
Write a list of strings to a file with UTF-8 charset- Parameters:
file- File to write todata- List of strings to write
-
write
Write list of strings to a file with the specified charset- Parameters:
file- File to write todata- List of strings to writepCharset- Charset to write with
-
writeAsMap
-
getListOfFilesMatchingExt
Returns an empty list if no files are found.- Parameters:
pDirectory-pExtension-- Returns:
-
getListOfFilesMatchingExt
Don't put the . on it. The . is added for you. So, you would pass in "java" to find *.java.- Parameters:
sourceFolder-pExtension- - NO PERIOD.- Returns:
-
getListOfFilesMatchingSuffix
Finds files that end with suffix. This is case-sensitive. You can use this for extension by starting the suffix with a .- Parameters:
sourceFolder-suffix-- Returns:
-
getListOfFilesMatchingCaseInsensitiveSuffix
public static Collection<File> getListOfFilesMatchingCaseInsensitiveSuffix(File sourceFolder, String suffix) Finds files that end with suffix. This is case-sensitive. You can use this for extension by starting the suffix with a .- Parameters:
sourceFolder-suffix-- Returns:
-
getListOfFilesMatchingRegex
-
getListOfFilesMatchingWildcard
public static Collection<File> getListOfFilesMatchingWildcard(File sourceFolder, String wildCardExpression) -
zipInputStreamsToOutputStream
public static ByteArrayOutputStream zipInputStreamsToOutputStream(Map<String, InputStream> pInputStreams) throws IOExceptionCombines multiple input streams into a single zip stream- Parameters:
pInputStreams- Map of [fileName: InputStream] to include in the zip- Returns:
- ZipOutputStream of the zip
- Throws:
IOException
-
zipInputStreamsToInputStream
public static InputStream zipInputStreamsToInputStream(Map<String, InputStream> pInputStreams) throws IOExceptionCombines multiple input streams into a single compressed input stream- Parameters:
pInputStreams- Map of [fileName: InputStream] to include in the zip- Returns:
- InputStream of the combined streams. This can be used for api upload or file writing
- Throws:
IOException
-
zip
- Throws:
FlexCheckedException
-
unZip
- Throws:
FlexCheckedException
-
unZip
- Throws:
FlexCheckedException
-
unZip
- Throws:
FlexCheckedException
-
unZipJar
public static void unZipJar(String pJarFile, String pInputFolder, String pOutputFolder) throws FlexCheckedException - Throws:
FlexCheckedException
-
unZipJar
public static void unZipJar(String pJarFileWithPath, String pOutputFolder) throws FlexCheckedException - Throws:
FlexCheckedException
-
zipDirectory
public static void zipDirectory(String pNamePrefix, File pDirectory, ZipOutputStream pZos) throws IOException Add a directory to a ZipOutputStream. Useful if you want to zip some directories in a folder but not all- Parameters:
pNamePrefix- Name of the directorypDirectory- Directory to zippZos- Already opened ZipOutputStream. CALLER MUST CLOSE THIS STREAM.- Throws:
IOException
-
zipFile
public static void zipFile(String pNamePrefix, File pFile, ZipOutputStream pZipOutputStream) throws IOException Add a file into a zip output stream- Parameters:
pNamePrefix- directory before the filepFile- The file to addpZipOutputStream- The stream to add the file too. Caller needs to close the stream.- Throws:
IOException
-
convertGlobToRegEx
-
calculateDirectoryHash
Calculate directory hash by considering all files in the folder.- Parameters:
pDirToHash-- Returns:
- Throws:
FlexCheckedException
-
calculateFileHash
Calculates hash based on file content.This method was copied from ArtifactUtil of FlexDeployPluginCore.
- Parameters:
pFile-- Returns:
- Throws:
FlexCheckedException
-
hashInputStream
public static String hashInputStream(InputStream inputStream, String pFileName) throws FlexCheckedException - Throws:
FlexCheckedException
-
convertByteArrayToHexString
-
probeContentType
in Java 11+, need to include javax.activation as a dependency to use this method. -
probeContentType
-
probeContentType
-
convertFileToBase64String
-
convertFileToBase64String
-
convertBase64EncodedStringToFile
public static void convertBase64EncodedStringToFile(String pEncodedString, File pFile) throws FlexCheckedException Deprecated.Use java 8 Base64 when java 6 dependency is removed.- Parameters:
pEncodedString-pFile-- Throws:
FlexCheckedException
-
convertBase64EncodedString
-
getExtensionFromFileName
-
getExtensionFromFileName
-
getQuotedFilePath
Returns a properly quoted file path, depending on the operating system.- Parameters:
pFile- The path of the file- Returns:
- on Linux The path is surrounded with single quotes (' '), and any existing single quotes inside the path are escaped using '\''
- Throws:
FlexRuntimeException- ifpFileis null.
-
getSafeFileName
Returns a filename that is safe for use on all operating systems. Invalid characters will be replaced with a space- Parameters:
pFileName-- Returns:
-
getSafeFileName
Returns a filename that is safe for use on all operating systems.- Parameters:
pFileName-pReplacement- Character to replace invalid characters with- Returns:
-