net.sf.okapi.common

## Class FileUtil

- java.lang.Object
- - net.sf.okapi.common.FileUtil

-     public final class FileUtil
      extends java.lang.Object

  Helper methods for manipulating files.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUtil()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `addFilesToZip(java.io.File zipFile, java.io.File[] files)`
    `static void` | `addFileToZip(java.io.File zipFile, java.lang.String file)`
    `static java.io.File` | `createTempFile(java.lang.String prefix)`
    `static java.io.File` | `createTempFile(java.lang.String prefix, java.lang.String extension)`
    `static void` | `deleteAllFilesInDirectory(java.lang.String directoryPath)` Deletes all files in the specified directory.
    `static void` | `deleteFileFromZip(java.io.File zipFile, java.lang.String file)`
    `static void` | `deleteFilesFromZip(java.io.File zipFile, java.lang.String[] files)`
    `static java.net.URL` | `fileToUrl(java.io.File file)`
    `static java.util.Collection<java.io.File>` | `getFilteredFiles(java.io.File directory, java.io.FilenameFilter filter, boolean recurse)` Gets a collection of the files in a given directory.
    `static java.io.File[]` | `getFilteredFilesAsArray(java.io.File directory, java.io.FilenameFilter filter, boolean recurse)` Gets an array of the files in a given directory.
    `static java.lang.String` | `getLocaleBasedFile(java.lang.String baseName, java.lang.String extension, LocaleId locale)` Return a path to a locale based resource using the standard java property resource resolution.
    `static java.lang.String` | `getPartBeforeFile(java.net.URI uri)` Gets the URI part before the file name.
    `static java.util.List<java.lang.String>` | `guessLanguages(java.io.Reader reader)` Tries to guess the language(s) declared in the given input.
    `static java.util.List<java.lang.String>` | `guessLanguages(java.lang.String path)` Tries to guess the language(s) declared in the given input file.
    `static boolean` | `isXliffSegmented(java.io.Reader reader)` Scans xliff file to see if it is segmented (has <seg-source>)
    `static boolean` | `isXliffSegmented(java.lang.String path)` Scans xliff file to see if it is segmented (has <seg-source>)
    `static void` | `unzip(java.lang.String zipFileName, java.lang.String destPath)` Extract a given ZIP file to a given destination folder.
    `static java.io.File` | `urlToFile(java.net.URL url)`
    `static void` | `zipDirectory(java.lang.String sourceDir, java.lang.String zipExtension)` Compresses a given directory.
    `static void` | `zipDirectory(java.lang.String sourceDir, java.lang.String zipExtension, java.lang.String destinationPathWithoutExtension)` Compresses a given directory.
    `static void` | `zipFiles(java.lang.String zipPath, java.lang.String sourceDir, java.lang.String... filenames)` Creates a ZIP file and adds a list of files in it.

    - ### 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

    - #### getFilteredFilesAsArray

          public static java.io.File[] getFilteredFilesAsArray(java.io.File directory,
                                                               java.io.FilenameFilter filter,
                                                               boolean recurse)

      Gets an array of the files in a given directory.

      This method searches all `File`s recursively that pass the `FilenameFilter`. Adapted from http://snippets.dzone.com/posts/show/1875

      Parameters:
        `directory` - root directory
        `filter` - `FilenameFilter` used to filter the File candidates
        `recurse` - true to recurse in the sub-directories, false to not.
      Returns:
        an array of `File`s (File[])

    - #### getFilteredFiles

          public static java.util.Collection<java.io.File> getFilteredFiles(java.io.File directory,
                                                                            java.io.FilenameFilter filter,
                                                                            boolean recurse)

      Gets a collection of the files in a given directory.

      This method search all `File`s recursively that pass the `FilenameFilter`. Adapted from http://snippets.dzone.com/posts/show/1875

      Parameters:
        `directory` - root directory
        `filter` - `FilenameFilter` used to filter the File candidates
        `recurse` - true to recurse in the sub-directories, false to not.
      Returns:
        `Collection` of `File`s

    - #### guessLanguages

          public static java.util.List<java.lang.String> guessLanguages(java.lang.String path)

      Tries to guess the language(s) declared in the given input file. The method should work with XLIFF, TMX, TTX and TS files.

      The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.

      The encoding for the file is determined based on the BOM, if present.

      Parameters:
        `path` - the full path of the file to process.
      Returns:
        a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.

    - #### guessLanguages

          public static java.util.List<java.lang.String> guessLanguages(java.io.Reader reader)

      Tries to guess the language(s) declared in the given input. The method should work with XLIFF, TMX, TTX and TS files.

      The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.

      Parameters:
        `reader` - a reader providing the content to examine. This reader will be closed by this method.
      Returns:
        a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.

    - #### isXliffSegmented

          public static boolean isXliffSegmented(java.lang.String path)

      Scans xliff file to see if it is segmented (has <seg-source>)
      Parameters:
        `path` - the full path of the xliff file to process.
      Returns:
        a true if xliff is segmented, false otherwise.

    - #### isXliffSegmented

          public static boolean isXliffSegmented(java.io.Reader reader)

      Scans xliff file to see if it is segmented (has <seg-source>)
      Parameters:
        `reader` - a reader providing the xliff content to examine. This reader will be closed by this method.
      Returns:
        a true if xliff is segmented, false otherwise.

    - #### zipDirectory

          public static void zipDirectory(java.lang.String sourceDir,
                                          java.lang.String zipExtension)

      Compresses a given directory. Creates in the same parent folder a ZIP file with the folder name as the file name and a given extension. The given directory is not deleted after compression.

      This method uses the Java ZIP package and does not supports files to zip that have a path with extended characters.

      Parameters:
        `sourceDir` - the given directory to be compressed
        `zipExtension` - an extension for the output ZIP file (default is .zip if a null or empty string is passed by the caller). The extension is expected to contain the leading period.

    - #### zipDirectory

          public static void zipDirectory(java.lang.String sourceDir,
                                          java.lang.String zipExtension,
                                          java.lang.String destinationPathWithoutExtension)

      Compresses a given directory. The given directory is not deleted after compression.

      This method uses the Java ZIP package and does not supports files to zip that have a path with extended characters.

      Parameters:
        `sourceDir` - the given directory to be compressed
        `zipExtension` - an extension for the output ZIP file (default is .zip if a null or empty string is passed by the caller). The extension is expected to contain the leading period.
        `destinationPathWithoutExtension` - output path of the zip file, without extension. Use null to use the source directory path.

    - #### zipFiles

          public static void zipFiles(java.lang.String zipPath,
                                      java.lang.String sourceDir,
                                      java.lang.String... filenames)

      Creates a ZIP file and adds a list of files in it.
      Parameters:
        `zipPath` - the path of the ZIP file to create.
        `sourceDir` - the path of the directory where the source files are located.
        `filenames` - the list of files to zip.

    - #### addFileToZip

          public static void addFileToZip(java.io.File zipFile,
                                          java.lang.String file)
                                   throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### addFilesToZip

          public static void addFilesToZip(java.io.File zipFile,
                                           java.io.File[] files)
                                    throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### unzip

          public static void unzip(java.lang.String zipFileName,
                                   java.lang.String destPath)

      Extract a given ZIP file to a given destination folder. From http://www.java2s .com/Code/Java/File-Input-Output/Extractzipfiletodestinationfolder.htm
      Parameters:
        `zipFileName` - full path of the given ZIP file
        `destPath` - destination folder

    - #### deleteAllFilesInDirectory

          public static void deleteAllFilesInDirectory(java.lang.String directoryPath)

      Deletes all files in the specified directory.
      Parameters:
        `directoryPath` - - the path to the directory
      Throws:
        `OkapiIOException` - if a file cannot be deleted.

    - #### getLocaleBasedFile

          public static java.lang.String getLocaleBasedFile(java.lang.String baseName,
                                                            java.lang.String extension,
                                                            LocaleId locale)

      Return a path to a locale based resource using the standard java property resource resolution. Works with any kind of files e.g., segmenter_en_US.srx or content_fr_FR.html

      WARNING: Assumes default classLoader only!!

      Parameters:
        `baseName` - base name of the resource
        `extension` - resource file extension
        `locale` - locale of the resource we are looking for
      Returns:
        the path to the resource or null if not found

    - #### getPartBeforeFile

          public static java.lang.String getPartBeforeFile(java.net.URI uri)

      Gets the URI part before the file name.
      Parameters:
        `uri` - The URI to process.
      Returns:
        the URI part before the file name.

    - #### createTempFile

          public static java.io.File createTempFile(java.lang.String prefix,
                                                    java.lang.String extension)

    - #### createTempFile

          public static java.io.File createTempFile(java.lang.String prefix)

    - #### deleteFileFromZip

          public static void deleteFileFromZip(java.io.File zipFile,
                                               java.lang.String file)
                                        throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### deleteFilesFromZip

          public static void deleteFilesFromZip(java.io.File zipFile,
                                                java.lang.String[] files)
                                         throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### fileToUrl

          public static java.net.URL fileToUrl(java.io.File file)

    - #### urlToFile

          public static java.io.File urlToFile(java.net.URL url)