org.knime.core.util
Class FileUtil

java.lang.Object
  extended by org.knime.core.util.FileUtil

public final class FileUtil
extends Object

Utility class to do some basic file handling that is not available through java API. This includes copying of files and deleting entire directories. These methods are mainly used for the load/save of the workflow.

Author:
Bernd Wiswedel, University of Konstanz

Nested Class Summary
static interface FileUtil.ZipFileFilter
          Passed to the zipDir(File, Collection, int, ZipFileFilter, ExecutionMonitor) method to exclude certain files from being archived and added to the zip file.
A default implementation accepting all files is ZIP_INCLUDEALL_FILTER
 
Field Summary
static FileUtil.ZipFileFilter ZIP_INCLUDEALL_FILTER
          A filter that causes all files to be included in the zip archive.
 
Method Summary
static boolean chmod(File f, Boolean readable, Boolean writable, Boolean executable, boolean ownerOnly)
          Sets the permissions on a given file or directory.
static void copy(File file, File destination)
          Copies a file.
static void copy(File file, File destination, ExecutionMonitor exec)
          Copies a file.
static void copy(InputStream input, OutputStream destination)
          Copies the bytes as read from input to the output stream destination.
static void copy(Reader source, Writer destination)
          Copies the chars as read from source to the writer destination.
static void copyDir(File sourceDir, File targetDir)
          Copies the given source (either a file or a directory) into the given source.
static File createTempDir(String prefix)
          Creates a temporary directory that is automatically deleted when the JVM shuts down.
static File createTempDir(String prefix, File dir)
          Creates a temporary directory that is automatically deleted when the JVM shuts down.
static boolean deleteRecursively(File dir)
          Deletes a given directory recursively.
static void unzip(File zipFile, File destDir)
          Extracts the contents of the given ZIP file into the destination directory.
static void unzip(ZipInputStream zipStream, File dir, int stripLevel)
          Stores the content of the zip stream in the specified directory.
static boolean zipDir(File zipFile, Collection<File> includeList, int compressionLevel, FileUtil.ZipFileFilter filter, ExecutionMonitor exec)
          Packs all files and directories passed in the includeList into a zip file.
static boolean zipDir(File zipFile, File rootDir, int compressionLevel)
          Recursively packs all the the files and directories beneath the rootDir into a zip file.
static boolean zipDir(File zipFile, File rootDir, int compressionLevel, FileUtil.ZipFileFilter filter, ExecutionMonitor exec)
          Recursively packs all the the files and directories beneath the rootDir into a zip file.
static boolean zipDir(ZipOutputStream zout, Collection<File> includeList, FileUtil.ZipFileFilter filter, ExecutionMonitor exec)
          Packs all files and directories passed in the includeList into a zip stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZIP_INCLUDEALL_FILTER

public static final FileUtil.ZipFileFilter ZIP_INCLUDEALL_FILTER
A filter that causes all files to be included in the zip archive.

Method Detail

copy

public static void copy(File file,
                        File destination,
                        ExecutionMonitor exec)
                 throws IOException,
                        CanceledExecutionException
Copies a file. The implementation uses a temporary buffer of 8kB. This method will report progress to the given execution monitor and will also check a canceled status. The copy process will report progress in the full range of the execution monitor. Consider to use a sub-execution monitor if the copy process is only a small part of the entire work.

Parameters:
file - The file to copy.
destination - The destination file, fully qualified (do not provide a directory).
exec - The execution monitor for progress information.
Throws:
CanceledExecutionException - If canceled. The destination file will be deleted.
IOException - If that fail for any reason.

copyDir

public static void copyDir(File sourceDir,
                           File targetDir)
                    throws IOException
Copies the given source (either a file or a directory) into the given source. If the source file or directory exist, it will be removed first. File permissions are not handled explicitly.

Parameters:
sourceDir - contains all source file and directories to be copied
targetDir - target file (created or replaced) with the given source file structure
Throws:
IOException - if the source does not exist or the source could not be copied due to file permissions

copy

public static void copy(InputStream input,
                        OutputStream destination)
                 throws IOException
Copies the bytes as read from input to the output stream destination. Neither input nor destination get closed at the end!

Parameters:
input - To read from
destination - To write to
Throws:
IOException - If that fails for any reason.
NullPointerException - If any argument is null.

copy

public static void copy(Reader source,
                        Writer destination)
                 throws IOException
Copies the chars as read from source to the writer destination. Neither input nor destination get closed at the end!

Parameters:
source - To read from
destination - To write to
Throws:
IOException - If that fails for any reason.
NullPointerException - If any argument is null.

copy

public static void copy(File file,
                        File destination)
                 throws IOException
Copies a file. The implementation uses a temporary buffer of 8kB.

Parameters:
file - The file to copy.
destination - The destination file, fully qualified (do not provide a directory).
Throws:
IOException - If that fail for any reason.

deleteRecursively

public static boolean deleteRecursively(File dir)
Deletes a given directory recursively. If the argument represents a file, the file will be deleted. If it represents a symbolic link, it won't follow the link but simply delete the link. Links contained in any of the subdirectories are deleted without touching the link source.

Parameters:
dir - The directory or file to delete.
Returns:
If that was successful.

zipDir

public static boolean zipDir(File zipFile,
                             Collection<File> includeList,
                             int compressionLevel,
                             FileUtil.ZipFileFilter filter,
                             ExecutionMonitor exec)
                      throws IOException,
                             CanceledExecutionException
Packs all files and directories passed in the includeList into a zip file. Recursively adds all files contained in directories. Files in the include list are placed in the root of the archive. Files and directories in the include list must not have the same (simple) name - otherwise an I/O Exception is thrown.

Parameters:
zipFile - the zip file that should be created. If it exists it will be overwritten.
includeList - list of files or directories to add to the zip archive. Directories will be added with their content (recursively). Files are placed in the root of the archive (i.e. their path is not preserved). If entries have the same (simple) name, an I/O Exception is thrown.
compressionLevel - the desired compression level, see ZipOutputStream.setLevel(int)
filter - each file (and directory) contained is only included in the zip archive if it is accepted by the filter. If a directory is not accepted, it entire content is excluded from the zip. Must not be null.
exec - receives progress messages and is checked for cancel requests. Optional, can be null.
Returns:
true if all files and dirs accepted by the filter are included, false if an error occurs reading a file, if a directory is unreadable.
Throws:
CanceledExecutionException - if the operation was canceled through the exec
IOException - if an I/O error occurs when writing the zip file, or if two files or directories in the include list have the same (simple) name.

zipDir

public static boolean zipDir(ZipOutputStream zout,
                             Collection<File> includeList,
                             FileUtil.ZipFileFilter filter,
                             ExecutionMonitor exec)
                      throws IOException,
                             CanceledExecutionException
Packs all files and directories passed in the includeList into a zip stream. Recursively adds all files contained in directories. Files in the include list are placed in the root of the archive. Files and directories in the include list must not have the same (simple) name - otherwise an I/O Exception is thrown. The passed stream is not closed when the method returns. The stream should have the appropriate compression level set.

Parameters:
zout - a zipped output stream. Zip entries for each file are added to the stream. The compression level is not changed by this method. The stream remains open after the method returns!
includeList - list of files or directories to add to the zip archive. Directories will be added with their content (recursively). Files are placed in the root of the archive (i.e. their path is not preserved).
filter - each file (and directory) contained is only included in the zip archive if it is accepted by the filter. If a directory is not accepted, it entire content is excluded from the zip. Must not be null.
exec - receives progress messages and is checked for cancel requests. Optional, can be null.
Returns:
true if all files and dirs accepted by the filter are included, false if an error occurs reading a file in a directory, if a directory is unreadable.
Throws:
CanceledExecutionException - if the operation was canceled through the exec
IOException - if an I/O error occurs when writing the zip file, or if two files or directories in the include list have the same (simple) name, or an element in the include list doesn't exist.

zipDir

public static boolean zipDir(File zipFile,
                             File rootDir,
                             int compressionLevel)
                      throws IOException
Recursively packs all the the files and directories beneath the rootDir into a zip file. The zip file contains the root directory as the only entry in its root.

Parameters:
zipFile - the zip file that should be created. If it exists it will be overwritten.
rootDir - the directory to pack.
compressionLevel - the desired compression level, see ZipOutputStream.setLevel(int)
Returns:
true if all files and dirs are included, false if an error occurs reading a file or if a directory is unreadable.
Throws:
IOException - if an I/O error occurs

zipDir

public static boolean zipDir(File zipFile,
                             File rootDir,
                             int compressionLevel,
                             FileUtil.ZipFileFilter filter,
                             ExecutionMonitor exec)
                      throws IOException,
                             CanceledExecutionException
Recursively packs all the the files and directories beneath the rootDir into a zip file. The zip file contains the root directory as the only entry in its root.

Parameters:
zipFile - the zip file that should be created. If it exists it will be overwritten.
rootDir - the directory to pack.
compressionLevel - the desired compression level, see ZipOutputStream.setLevel(int)
filter - each file (and dir) contained in the rootDir is only included in the zip archive if it is accepted by the filter. The rootDir is always included. Files from the addRootFiles list are also not filtered. Must not be null.
exec - receives progress messages and is checked for cancel requests. Optional, can be null.
Returns:
true if all files and dirs are included, false if an error occurs reading a file, if a directory is unreadable, or a file in the addRootFiles list is a directory, not readable or doesn't exist.
Throws:
CanceledExecutionException - if the operation was canceled through the exec
IOException - if an I/O error occurs

unzip

public static void unzip(File zipFile,
                         File destDir)
                  throws IOException
Extracts the contents of the given ZIP file into the destination directory.

Parameters:
zipFile - a ZIP file
destDir - the destination directory, must already exist
Throws:
IOException - if an I/O error occurs

unzip

public static void unzip(ZipInputStream zipStream,
                         File dir,
                         int stripLevel)
                  throws IOException
Stores the content of the zip stream in the specified directory. If a strip level larger than zero is specified, it strips off that many path segments from the zip entries. If the zip stream contains elements with less path segments, they all end up directly in the specified dir.

Parameters:
zipStream - must contain a zip archive. Is unpacked an stored in the specified directory.
dir - the destination directory the content of the zip stream is stored in
stripLevel - the number of path segments (directory levels) striped off the file (and dir) names in the zip archive.
Throws:
IOException - if it was not able to store the content

createTempDir

public static File createTempDir(String prefix)
                          throws IOException
Creates a temporary directory that is automatically deleted when the JVM shuts down.

Parameters:
prefix - the prefix string to be used in generating the file's name
Returns:
an abstract pathname denoting a newly-created empty directory
Throws:
IOException - if the directory could not be created

createTempDir

public static File createTempDir(String prefix,
                                 File dir)
                          throws IOException
Creates a temporary directory that is automatically deleted when the JVM shuts down.

Parameters:
prefix - the prefix string to be used in generating the file's name
dir - the directory in which the file is to be created, or null if the default temporary-file directory is to be used
Returns:
an abstract pathname denoting a newly-created empty directory
Throws:
IOException - if the directory could not be created

chmod

public static boolean chmod(File f,
                            Boolean readable,
                            Boolean writable,
                            Boolean executable,
                            boolean ownerOnly)
Sets the permissions on a given file or directory. If a directory is specified it recursively sets the permissions on it and all contained files or directories.

Parameters:
f - a file or directory to change the permissions on (recursively).
readable - if the readable-bit should be set, or null if its value shouldn't be changed
writable - if the writable-bit should be set, or null if its value shouldn't be changed
executable - if the executable-bit should be set, or null if its value shouldn't be changed
ownerOnly - If true, the read permission applies only to the owner's read permission; otherwise, it applies to everybody. If the underlying file system can not distinguish the owner's read permission from that of others, then the permission will apply to everybody, regardless of this value.
Returns:
true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname.


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.