Package org.qubership.atp.mia.utils
Class FileUtils
java.lang.Object
org.qubership.atp.mia.utils.FileUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddDirToZipArchive(ZipOutputStream zos, File fileToZip, String parentDirectoryName, org.apache.commons.io.filefilter.IOFileFilter filter) Saves selectedfileToZipto zip output stream (zip archive).static voidCopy file.static voidcopyFilesFromCurrentDir(File src, File dest) Copy files from current directory to destination folder.static voidcopyFolder(Path src, Path dest) Copy folder.static voidcreateDirectories(Path pathToCreate) Creates directories.static voidcreateFolder(File targetFile) Create folder.static voiddeleteFolder(File dir, boolean deleteNowDir) Deletes a folder content and the folder itself if deleteNowDir parameter istrue.static voiddeleteFolders(boolean deleteNowDir, File[] dirs) Takes every directory from dirs parameter and clean this directory withdeleteFoldermethod.static FilefindFileAndGetParent(File inputDir, String searchDirName) Finds directory which name equals tosearchDirName.static PathgetPathToFileFromFile(File file) Parses the file path and removes till flow folder.static voidlogIntoFile(String logString, File file) Log information into file.static FileCreate file with protection from ZipSlip vulnerability, so it is not possible to create outside the current dir.static StringReads file by name from directory of flow config.static voidremoveFile(String fileToDelete) Remove file.static StringtempFileName(String dir, String fileName) Correct path to file.static FileunzipConfig(File zipPath, File destDir) Unzip archive into the destination directory.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
logIntoFile
Log information into file.- Parameters:
logString- Log informationfile- file
-
addDirToZipArchive
public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parentDirectoryName, org.apache.commons.io.filefilter.IOFileFilter filter) Saves selectedfileToZipto zip output stream (zip archive). This operation done recursively.- Parameters:
zos- zip output stream, which should be open.fileToZip- this file (or directory) will be stored in archive.parentDirectoryName- used in next cycles of recursion, could benull.filter- used to filter file/directories
-
unzipConfig
Unzip archive into the destination directory.- Throws:
IOException
-
newFile
Create file with protection from ZipSlip vulnerability, so it is not possible to create outside the current dir.- Parameters:
destinationDir- dir to create.zipEntry- opened zip archive.- Returns:
Fileof created dir.- Throws:
IOException- if the created entry is outside of the target dir.
-
copyFilesFromCurrentDir
Copy files from current directory to destination folder.- Parameters:
src- fromdest- to- Throws:
IOException- IOException during coping files
-
deleteFolder
Deletes a folder content and the folder itself if deleteNowDir parameter istrue.- Parameters:
dir- dir which should be cleaned.deleteNowDir- if true then delete root dir, otherwise keeps it (with cleaned content).
-
deleteFolders
Takes every directory from dirs parameter and clean this directory withdeleteFoldermethod.- Parameters:
deleteNowDir- if true then delete root dir, otherwise keeps it (with cleaned content). Affects every directory from dirs parameter.dirs- array with directories to clean.
-
findFileAndGetParent
public static File findFileAndGetParent(File inputDir, String searchDirName) throws FileNotFoundException Finds directory which name equals tosearchDirName. Search occurs ininputDirdirectory and its subdirectories. Very handy to use it in case to find flow directory in config.- Parameters:
inputDir- directory where search occur.searchDirName- name of searched directory.- Returns:
Filewhich linked to directory or exception thrown.- Throws:
FileNotFoundException- in case when file not found the exception thrown.
-
readFile
Reads file by name from directory of flow config. -
copyFolder
Copy folder.- Parameters:
src- fromdest- to- Throws:
IOException- IOException during coping files
-
copyFile
Copy file.- Parameters:
src- fromdest- to- Throws:
IOException- IOException during coping files
-
removeFile
Remove file.- Parameters:
fileToDelete- fileToDelete
-
createDirectories
Creates directories.- Parameters:
pathToCreate- pathToCreate
-
tempFileName
Correct path to file.- Parameters:
dir- dirfileName- file name- Returns:
- correct path to file
-
getPathToFileFromFile
Parses the file path and removes till flow folder. If file directly inside flow, it gives file name. Ex: Template_SSH.json Else if file is inside subfolder of flow folder, then it gives the path from flow directory. Ex: ./SSH/SSH.json- Parameters:
file- actualFile- Returns:
- correct path to file,
-
createFolder
Create folder.
-