Class FileUtils

java.lang.Object
org.qubership.atp.mia.utils.FileUtils

public class FileUtils extends Object
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • logIntoFile

      public static void logIntoFile(String logString, File file)
      Log information into file.
      Parameters:
      logString - Log information
      file - file
    • addDirToZipArchive

      public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parentDirectoryName, org.apache.commons.io.filefilter.IOFileFilter filter)
      Saves selected fileToZip to 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 be null.
      filter - used to filter file/directories
    • unzipConfig

      public static File unzipConfig(File zipPath, File destDir) throws IOException
      Unzip archive into the destination directory.
      Throws:
      IOException
    • newFile

      public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException
      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:
      File of created dir.
      Throws:
      IOException - if the created entry is outside of the target dir.
    • copyFilesFromCurrentDir

      public static void copyFilesFromCurrentDir(File src, File dest) throws IOException
      Copy files from current directory to destination folder.
      Parameters:
      src - from
      dest - to
      Throws:
      IOException - IOException during coping files
    • deleteFolder

      public static void deleteFolder(File dir, boolean deleteNowDir)
      Deletes a folder content and the folder itself if deleteNowDir parameter is true.
      Parameters:
      dir - dir which should be cleaned.
      deleteNowDir - if true then delete root dir, otherwise keeps it (with cleaned content).
    • deleteFolders

      public static void deleteFolders(boolean deleteNowDir, File[] dirs)
      Takes every directory from dirs parameter and clean this directory with deleteFolder method.
      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 to searchDirName. Search occurs in inputDir directory 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:
      File which linked to directory or exception thrown.
      Throws:
      FileNotFoundException - in case when file not found the exception thrown.
    • readFile

      public static String readFile(Path pathToFile)
      Reads file by name from directory of flow config.
    • copyFolder

      public static void copyFolder(Path src, Path dest) throws IOException
      Copy folder.
      Parameters:
      src - from
      dest - to
      Throws:
      IOException - IOException during coping files
    • copyFile

      public static void copyFile(Path src, Path dest) throws IOException
      Copy file.
      Parameters:
      src - from
      dest - to
      Throws:
      IOException - IOException during coping files
    • removeFile

      public static void removeFile(String fileToDelete)
      Remove file.
      Parameters:
      fileToDelete - fileToDelete
    • createDirectories

      public static void createDirectories(Path pathToCreate)
      Creates directories.
      Parameters:
      pathToCreate - pathToCreate
    • tempFileName

      @Nonnull public static String tempFileName(String dir, String fileName)
      Correct path to file.
      Parameters:
      dir - dir
      fileName - file name
      Returns:
      correct path to file
    • getPathToFileFromFile

      public static Path getPathToFileFromFile(File file)
      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

      public static void createFolder(File targetFile)
      Create folder.