Class FileUtil


  • public final class FileUtil
    extends java.lang.Object
    File Utilities
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String module  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsString​(java.io.Reader reader, java.lang.String searchString)
      Search for the specified searchString in the given Reader.
      static boolean containsString​(java.lang.String fileName, java.lang.String searchString)
      Search for the specified searchString in the given filename.
      static java.util.List<java.io.File> findFiles​(java.lang.String fileExt, java.lang.String basePath, java.lang.String partialPath, java.lang.String stringToFind)  
      static java.util.List<java.io.File> findXmlFiles​(java.lang.String basePath, java.lang.String partialPath, java.lang.String rootElementName, java.lang.String xsdOrDtdName)  
      static java.io.OutputStream getBufferedOutputStream​(java.lang.String path, java.lang.String name)  
      static java.io.Writer getBufferedWriter​(java.lang.String path, java.lang.String name)  
      static java.io.File getFile​(java.io.File root, java.lang.String path)  
      static java.io.File getFile​(java.lang.String path)  
      static java.lang.String getPatchedFileName​(java.lang.String path, java.lang.String fileName)  
      static boolean isFile​(java.lang.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.
      static java.lang.String localizePath​(java.lang.String path)
      Converts a file path to one that is compatible with the host operating system.
      static java.io.File newFile​(java.lang.String destinationDirName, java.util.zip.ZipEntry zipEntry)
      This useful to prevents Zip slip vulnerability cf.
      static java.io.File normalizeFilePath​(java.lang.String filePath)
      Creates a File with a normalized file path This useful to prevent path traversal security issues cf.
      static java.lang.String readString​(java.lang.String encoding, java.io.File inFile)  
      static java.lang.StringBuffer readTextFile​(java.io.File file, boolean newline)  
      static java.lang.StringBuffer readTextFile​(java.lang.String fileName, boolean newline)  
      static void searchFiles​(java.util.List<java.io.File> fileList, java.io.File path, java.io.FilenameFilter filter, boolean includeSubfolders)  
      static boolean unZip​(java.lang.String source, java.lang.String destination, java.lang.String password)
      Unzip file structure of the given zipFile to specified outputFolder The Zip slip vulnerabilty is handled since version 1.3.3 of net.lingala.zip4j.ZipFile; unzipFileToFolder is not as reliable and does not handle passwords
      static boolean unzipFileToFolder​(java.io.File zipFile, java.lang.String outputFolder, boolean handleZipSlip)
      Unzip file structure of the given zipFile to specified outputFolder
      static void writeString​(java.lang.String fileName, java.lang.String s)  
      static void writeString​(java.lang.String encoding, java.lang.String s, java.io.File outFile)  
      static void writeString​(java.lang.String path, java.lang.String name, java.lang.String s)  
      static void writeString​(java.lang.String path, java.lang.String name, java.lang.String encoding, java.lang.String s)
      Writes a file from a string with a specified encoding.
      static java.io.ByteArrayInputStream zipFileStream​(java.io.InputStream fileStream, java.lang.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • module

        public static final java.lang.String module
    • Method Detail

      • getFile

        public static java.io.File getFile​(java.lang.String path)
      • getFile

        public static java.io.File getFile​(java.io.File root,
                                           java.lang.String path)
      • localizePath

        public static java.lang.String localizePath​(java.lang.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​(java.lang.String fileName,
                                       java.lang.String s)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • writeString

        public static void writeString​(java.lang.String path,
                                       java.lang.String name,
                                       java.lang.String s)
      • writeString

        public static void writeString​(java.lang.String path,
                                       java.lang.String name,
                                       java.lang.String encoding,
                                       java.lang.String s)
                                throws java.io.IOException
        Writes a file from a string with a specified encoding.
        Parameters:
        path -
        name -
        encoding -
        s -
        Throws:
        java.io.IOException
      • writeString

        public static void writeString​(java.lang.String encoding,
                                       java.lang.String s,
                                       java.io.File outFile)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • getBufferedWriter

        public static java.io.Writer getBufferedWriter​(java.lang.String path,
                                                       java.lang.String name)
                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • getBufferedOutputStream

        public static java.io.OutputStream getBufferedOutputStream​(java.lang.String path,
                                                                   java.lang.String name)
                                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • getPatchedFileName

        public static java.lang.String getPatchedFileName​(java.lang.String path,
                                                          java.lang.String fileName)
                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • readTextFile

        public static java.lang.StringBuffer readTextFile​(java.io.File file,
                                                          boolean newline)
                                                   throws java.io.FileNotFoundException,
                                                          java.io.IOException
        Throws:
        java.io.FileNotFoundException
        java.io.IOException
      • readTextFile

        public static java.lang.StringBuffer readTextFile​(java.lang.String fileName,
                                                          boolean newline)
                                                   throws java.io.FileNotFoundException,
                                                          java.io.IOException
        Throws:
        java.io.FileNotFoundException
        java.io.IOException
      • readString

        public static java.lang.String readString​(java.lang.String encoding,
                                                  java.io.File inFile)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • searchFiles

        public static void searchFiles​(java.util.List<java.io.File> fileList,
                                       java.io.File path,
                                       java.io.FilenameFilter filter,
                                       boolean includeSubfolders)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • findFiles

        public static java.util.List<java.io.File> findFiles​(java.lang.String fileExt,
                                                             java.lang.String basePath,
                                                             java.lang.String partialPath,
                                                             java.lang.String stringToFind)
                                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • findXmlFiles

        public static java.util.List<java.io.File> findXmlFiles​(java.lang.String basePath,
                                                                java.lang.String partialPath,
                                                                java.lang.String rootElementName,
                                                                java.lang.String xsdOrDtdName)
                                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • containsString

        public static boolean containsString​(java.io.Reader reader,
                                             java.lang.String searchString)
                                      throws java.io.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:
        java.io.IOException
      • containsString

        public static boolean containsString​(java.lang.String fileName,
                                             java.lang.String searchString)
                                      throws java.io.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:
        java.io.IOException
      • isFile

        public static boolean isFile​(java.lang.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 java.io.ByteArrayInputStream zipFileStream​(java.io.InputStream fileStream,
                                                                 java.lang.String fileName)
                                                          throws java.io.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:
        Throws:
        java.io.IOException
      • newFile

        public static java.io.File newFile​(java.lang.String destinationDirName,
                                           java.util.zip.ZipEntry zipEntry)
                                    throws java.io.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:
        java.io.IOException
      • unzipFileToFolder

        public static boolean unzipFileToFolder​(java.io.File zipFile,
                                                java.lang.String outputFolder,
                                                boolean handleZipSlip)
                                         throws java.io.IOException
        Unzip file structure of the given zipFile to specified outputFolder
        Parameters:
        zipFile -
        outputFolder -
        handleZipSlip - if true FileUtil::newFile is used
        Throws:
        java.io.IOException
      • normalizeFilePath

        public static java.io.File normalizeFilePath​(java.lang.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
      • unZip

        public static boolean unZip​(java.lang.String source,
                                    java.lang.String destination,
                                    java.lang.String password)
        Unzip file structure of the given zipFile to specified outputFolder The Zip slip vulnerabilty 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