Class FileUtil

java.lang.Object
org.apache.ofbiz.base.util.FileUtil

public final class FileUtil extends Object
File Utilities
  • Method Details

    • getFile

      public static File getFile(String path)
    • getFile

      public static File getFile(File root, String path)
    • localizePath

      public static String localizePath(String path)
      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

      public static void writeString(String fileName, String s) throws IOException
      Throws:
      IOException
    • writeString

      public static void writeString(String path, String name, String s)
    • 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

      public static void writeString(String encoding, String s, File outFile) throws IOException
      Throws:
      IOException
    • getBufferedWriter

      public static Writer getBufferedWriter(String path, String name) throws IOException
      Throws:
      IOException
    • getBufferedOutputStream

      public static OutputStream getBufferedOutputStream(String path, String name) throws IOException
      Throws:
      IOException
    • getPatchedFileName

      public static String getPatchedFileName(String path, String fileName) throws IOException
      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

      public static String readString(String encoding, File inFile) throws IOException
      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

      public static boolean containsString(Reader reader, String searchString) throws IOException
      Search for the specified searchString in the given Reader.
      Parameters:
      reader - A Reader in which the String will be searched.
      searchString - The String to search for
      Returns:
      TRUE if the searchString is found; FALSE otherwise.
      Throws:
      IOException
    • containsString

      public static boolean containsString(String fileName, String searchString) throws IOException
      Search for the specified searchString 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 the searchString is found; FALSE otherwise.
      Throws:
      IOException
    • isFile

      public static boolean isFile(String fileName)
      Check if the specified fileName exists and is a file (not a directory) If the specified file doesn't exist or is a directory FALSE returns.
      Parameters:
      fileName - A full path to a file in which the String will be searched.
      Returns:
      TRUE if the fileName 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

      public static File newFile(String destinationDirName, ZipEntry zipEntry) throws IOException
      This useful to prevents Zip slip vulnerability cf. https://snyk.io/research/zip-slip-vulnerability
      Parameters:
      destinationDirName - The file path name to normalize
      zipEntry - 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

      public static File createFileWithNormalizedPath(String filePath)
      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

      public static String normalizeFilePath(String filePath)
      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

      public static boolean unZip(String source, String destination, String password)
      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