Package lumis.util

Class FileUtil


  • @StableMinor(version="16.1",
                 sinceVersion="4.0")
    public class FileUtil
    extends java.lang.Object
    Utility methods for file manipulation.
    Since:
    4.0.0
    Version:
    $Revision: 26156 $ $Date: 2024-01-13 10:26:46 -0300 (Sat, 13 Jan 2024) $
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static boolean compareFiles​(java.io.File file1, java.io.File file2)
      Compare two files.
      static boolean compareStream​(java.io.InputStream in1, java.io.InputStream in2)  
      static void copyDir​(java.io.File srcDir, java.io.File destDir)
      Copies the source folder recursively to the destination folder.
      static void copyFile​(java.io.File srcFile, java.io.File destFile)
      Copies a file.
      static void copyStream​(java.io.InputStream in, java.io.OutputStream out)  
      static java.io.File createDir​(java.lang.String dirPath, boolean clearIfExists)  
      static java.io.File createFile​(java.lang.String filePath)  
      static java.io.FileOutputStream createFileOutputStream​(java.lang.String filePath)  
      static void createParentDirectories​(java.io.File file)
      Creates the directories parent of the given file.
      static boolean deleteDir​(java.io.File dir)  
      static boolean deleteDir​(java.io.File dir, boolean deleteFiles)  
      static boolean deleteDir​(java.lang.String dir)  
      static boolean deleteDir​(java.lang.String dir, boolean deleteFiles)  
      static boolean deleteFile​(java.io.File file)
      Deletes the given file.
      static boolean deleteFile​(java.lang.String filePath)  
      static void extractZip​(java.io.InputStream inputStream, java.io.File destDir)
      Extracts an input stream in ZIP format to the file system.
      static void extractZip​(java.io.InputStream inputStream, IFile destDir)
      Extracts an input stream in ZIP format to the file system.
      static void extractZip​(java.util.zip.ZipInputStream zipInput, java.io.File destDir)
      Deprecated.
      Since 7.0.0, replaced by extractZip(InputStream, File) due to limitations in Java's embedded java.util.zip package on working with different zip entry names encodings.
      static boolean fileExists​(java.lang.String filePath)  
      static java.lang.String getFileExtension​(java.lang.String fileName)
      Returns the extension of given file.
      static boolean move​(java.lang.String sourcePath, java.lang.String targetPath)  
      static byte[] readFile​(java.io.File file)
      Returns the content of a file as an array of bytes.
      static void zipDir​(java.io.File dirToZip, java.lang.String dirZipRelativePath, java.util.zip.ZipOutputStream zos)
      Zips a directory recursively into the given zip output stream.
      static void zipDir​(java.io.File dirToZip, java.util.zip.ZipOutputStream zos)
      Zips a directory recursively into the given zip output stream.
      static void zipDir​(IFile dirToZip, java.lang.String dirZipRelativePath, java.util.zip.ZipOutputStream zos)
      Zips a directory recursively into the given zip output stream.
      static void zipDir​(IFile dirToZip, java.util.zip.ZipOutputStream zos)
      Zips a directory recursively into the given zip output stream.
      • Methods inherited from class java.lang.Object

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

      • FileUtil

        public FileUtil()
    • Method Detail

      • move

        public static boolean move​(java.lang.String sourcePath,
                                   java.lang.String targetPath)
      • createFileOutputStream

        public static java.io.FileOutputStream createFileOutputStream​(java.lang.String filePath)
                                                               throws PortalException
        Throws:
        PortalException
      • createDir

        public static java.io.File createDir​(java.lang.String dirPath,
                                             boolean clearIfExists)
                                      throws PortalException
        Throws:
        PortalException
      • fileExists

        public static boolean fileExists​(java.lang.String filePath)
      • deleteDir

        public static boolean deleteDir​(java.lang.String dir)
      • deleteFile

        public static boolean deleteFile​(java.lang.String filePath)
      • deleteFile

        public static boolean deleteFile​(java.io.File file)
        Deletes the given file.
        Parameters:
        file - the file to be deleted.
        Returns:
        whether the action has been succeeded.
        Throws:
        java.lang.IllegalArgumentException - if the given argument is a directory.
      • deleteDir

        public static boolean deleteDir​(java.lang.String dir,
                                        boolean deleteFiles)
      • deleteDir

        public static boolean deleteDir​(java.io.File dir)
      • deleteDir

        public static boolean deleteDir​(java.io.File dir,
                                        boolean deleteFiles)
      • copyDir

        public static void copyDir​(java.io.File srcDir,
                                   java.io.File destDir)
                            throws java.io.IOException
        Copies the source folder recursively to the destination folder.
        Parameters:
        srcDir - the source folder
        destDir - the destination folder.
        Throws:
        java.io.IOException - if any error occurs during the read or write file.
        Since:
        7.1.0
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File destFile)
                             throws java.io.IOException
        Copies a file.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyStream

        public static void copyStream​(java.io.InputStream in,
                                      java.io.OutputStream out)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • compareFiles

        public static boolean compareFiles​(java.io.File file1,
                                           java.io.File file2)
                                    throws java.io.IOException
        Compare two files.
        Parameters:
        file1 -
        file2 -
        Returns:
        true, if the files' contents are identical.
        Throws:
        java.io.IOException
      • compareStream

        public static boolean compareStream​(java.io.InputStream in1,
                                            java.io.InputStream in2)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • extractZip

        @Deprecated
        public static void extractZip​(java.util.zip.ZipInputStream zipInput,
                                      java.io.File destDir)
                               throws java.io.IOException
        Deprecated.
        Since 7.0.0, replaced by extractZip(InputStream, File) due to limitations in Java's embedded java.util.zip package on working with different zip entry names encodings.
        Extracts a zip input stream into a directory.
        Parameters:
        zipInput - the zip input stream.
        destDir - the directory where the zip is to be extracted.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        4.0.7
      • extractZip

        public static void extractZip​(java.io.InputStream inputStream,
                                      java.io.File destDir)
                               throws java.io.IOException
        Extracts an input stream in ZIP format to the file system.
        Parameters:
        inputStream - the input stream. This is expected to be the raw input stream of the bytes in ZIP format, and must not be an input stream that already does the ZIP uncompressing such as ZipInputStream.
        destDir - the file system directory where the ZIP contents are to be extracted to.
        Throws:
        java.io.IOException - if an I/O error occurred while reading the input stream or writing to the file system.
        Since:
        7.0.0
      • extractZip

        public static void extractZip​(java.io.InputStream inputStream,
                                      IFile destDir)
                               throws java.io.IOException
        Extracts an input stream in ZIP format to the file system.
        Parameters:
        inputStream - the input stream. This is expected to be the raw input stream of the bytes in ZIP format, and must not be an input stream that already does the ZIP uncompressing such as ZipInputStream.
        destDir - the file system directory where the ZIP contents are to be extracted to.
        Throws:
        java.io.IOException - if an I/O error occurred while reading the input stream or writing to the file system.
        Since:
        8.0.0
      • zipDir

        public static void zipDir​(java.io.File dirToZip,
                                  java.util.zip.ZipOutputStream zos)
                           throws java.io.IOException
        Zips a directory recursively into the given zip output stream. The files in the given directory will be in the zip root.
        Parameters:
        dirToZip - the dir to zip.
        zos - the zip output stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        4.0.7
      • zipDir

        public static void zipDir​(java.io.File dirToZip,
                                  java.lang.String dirZipRelativePath,
                                  java.util.zip.ZipOutputStream zos)
                           throws java.io.IOException
        Zips a directory recursively into the given zip output stream.
        Parameters:
        dirToZip - the dir to zip.
        dirZipRelativePath - the path for the dir inside the generated zip file. If it refers to the zip root, its value must be an empty string; or it must end with slash if it refers to a directory inside the zip.
        zos - the zip output stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        4.0.7
      • zipDir

        public static void zipDir​(IFile dirToZip,
                                  java.util.zip.ZipOutputStream zos)
                           throws java.io.IOException
        Zips a directory recursively into the given zip output stream. The files in the given directory will be in the zip root.
        Parameters:
        dirToZip - the dir to zip.
        zos - the zip output stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        8.0.0
      • zipDir

        public static void zipDir​(IFile dirToZip,
                                  java.lang.String dirZipRelativePath,
                                  java.util.zip.ZipOutputStream zos)
                           throws java.io.IOException
        Zips a directory recursively into the given zip output stream.
        Parameters:
        dirToZip - the dir to zip.
        dirZipRelativePath - the path for the dir inside the generated zip file. If it refers to the zip root, its value must be an empty string; or it must end with slash if it refers to a directory inside the zip.
        zos - the zip output stream.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        8.0.0
      • createParentDirectories

        public static void createParentDirectories​(java.io.File file)
                                            throws java.io.IOException
        Creates the directories parent of the given file. If the parent directory already exist, nothing is done.
        Parameters:
        file - the file.
        Throws:
        java.io.IOException - if it was not possible to create the directories.
        Since:
        5.0.0
      • readFile

        public static byte[] readFile​(java.io.File file)
                               throws java.io.IOException
        Returns the content of a file as an array of bytes.
        Parameters:
        file - the file.
        Returns:
        the file's content.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        4.2.2
      • getFileExtension

        public static java.lang.String getFileExtension​(java.lang.String fileName)
        Returns the extension of given file.
        Parameters:
        fileName - the file name to used for extract the extension.
        Returns:
        the file extension.
        Since:
        7.0.0