com.isti.util
Class ZipFileUtils

java.lang.Object
  extended by com.isti.util.ZipFileUtils

public class ZipFileUtils
extends java.lang.Object

Class ZipFileUtils contains various static utility methods for working with ZIP files.


Field Summary
static java.lang.String[] FILE_EXTENSIONS
          Zip file extensions (in lower case.)
 
Method Summary
static boolean deleteDirectory(java.io.File path)
          Deletes a directory recursively.
static boolean extractFileFromZip(java.io.File zipFileObj, java.io.File destDirFileObj, java.lang.String fileNameStr)
          Extracts a file from the given '.zip' (or '.jar') archive to the given location.
static java.lang.String getSingleDirInZip(java.util.zip.ZipFile zipFileObj)
          Determines if the given archive contains entries residing in a single directory and returns the directory name if so.
static boolean hasZipExtension(java.lang.String name)
          Determines if the specified filename ends with a ZIP file extension.
static java.util.zip.ZipFile openZipFile(java.io.File file)
          Opens a ZIP file for reading given the specified File object.
static java.util.zip.ZipFile openZipFile(java.io.File file, int mode)
          Opens a new ZipFile to read from the specified File object in the specified mode.
static java.util.zip.ZipFile openZipFile(java.lang.String name)
          Opens a zip file for reading.
static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir)
          Unzips all entries from the zip file to the specified directory.
static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, boolean keepDateFlag)
          Unzips all entries from the zip file to the specified directory.
static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, boolean keepDateFlag, java.lang.String outputFilePrefix)
          Unzips all entries from the zip file to the specified directory.
static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, boolean keepDateFlag, java.lang.String outputFilePrefix, java.io.FileFilter filter, boolean junkPathsFlag)
          Unzips entries from the zip file to the specified directory.
static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, java.lang.String outputFilePrefix)
          Unzips all entries from the zip file to the specified directory.
static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, java.lang.String outputFilePrefix, java.io.FileFilter filter, boolean junkPathsFlag)
          Unzips entries from the zip file to the specified directory.
static java.lang.String zipAndDeleteDir(java.lang.String dirname, java.lang.String zipFile)
          from java2s.com online example - takes a directory, and zip's it up, and then deletes the dir from the disk
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_EXTENSIONS

public static java.lang.String[] FILE_EXTENSIONS
Zip file extensions (in lower case.)

Method Detail

hasZipExtension

public static boolean hasZipExtension(java.lang.String name)
Determines if the specified filename ends with a ZIP file extension.

Parameters:
name - the name of the zip file
Returns:
true if the specified filename ends with a ZIP file extension.

openZipFile

public static java.util.zip.ZipFile openZipFile(java.lang.String name)
                                         throws java.io.IOException
Opens a zip file for reading.

First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed.

Parameters:
name - the name of the zip file
Returns:
The zip file.
Throws:
java.util.zip.ZipException - if a ZIP format error has occurred
java.io.IOException - if an I/O error has occurred
java.lang.SecurityException - if a security manager exists and its checkRead method doesn't allow read access to the file.
See Also:
SecurityManager.checkRead(java.lang.String)

openZipFile

public static java.util.zip.ZipFile openZipFile(java.io.File file,
                                                int mode)
                                         throws java.io.IOException
Opens a new ZipFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE.

First, if there is a security manager, its checkRead method is called with the name argument as its argument to ensure the read is allowed.

Parameters:
file - the ZIP file to be opened for reading
mode - the mode in which the file is to be opened
Returns:
The zip file.
Throws:
java.util.zip.ZipException - if a ZIP format error has occurred.
java.io.IOException - if an I/O error has occurred.
java.lang.SecurityException - if a security manager exists and its checkRead method doesn't allow read access to the file.
java.lang.IllegalArgumentException - If the mode argument is invalid.
See Also:
SecurityManager.checkRead(java.lang.String)

openZipFile

public static java.util.zip.ZipFile openZipFile(java.io.File file)
                                         throws java.util.zip.ZipException,
                                                java.io.IOException
Opens a ZIP file for reading given the specified File object.

Parameters:
file - the ZIP file to be opened for reading
Returns:
The zip file.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred.
java.io.IOException - if an I/O error has occurred.

unzipZipFile

public static int unzipZipFile(java.util.zip.ZipFile zipFileObj,
                               java.io.File outputDir)
                        throws java.util.zip.ZipException
Unzips all entries from the zip file to the specified directory.

Parameters:
zipFileObj - the zip file.
outputDir - the output directory.
Returns:
The number of files unzipped.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred or if the output directory could not be created.

unzipZipFile

public static int unzipZipFile(java.util.zip.ZipFile zipFileObj,
                               java.io.File outputDir,
                               boolean keepDateFlag)
                        throws java.util.zip.ZipException
Unzips all entries from the zip file to the specified directory.

Parameters:
zipFileObj - the zip file.
outputDir - the output directory.
keepDateFlag - true to retain the last-modified date/time values for the unzipped files; false to use the current time.
Returns:
The number of files unzipped.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred or if the output directory could not be created.

unzipZipFile

public static int unzipZipFile(java.util.zip.ZipFile zipFileObj,
                               java.io.File outputDir,
                               java.lang.String outputFilePrefix)
                        throws java.util.zip.ZipException
Unzips all entries from the zip file to the specified directory.

Parameters:
zipFileObj - the zip file.
outputDir - the output directory.
outputFilePrefix - prefix to add to output filenames.
Returns:
The number of files unzipped.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred or if the output directory could not be created.

unzipZipFile

public static int unzipZipFile(java.util.zip.ZipFile zipFileObj,
                               java.io.File outputDir,
                               boolean keepDateFlag,
                               java.lang.String outputFilePrefix)
                        throws java.util.zip.ZipException
Unzips all entries from the zip file to the specified directory.

Parameters:
zipFileObj - the zip file.
outputDir - the output directory.
keepDateFlag - true to retain the last-modified date/time values for the unzipped files; false to use the current time.
outputFilePrefix - prefix to add to output filenames.
Returns:
The number of files unzipped.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred or if the output directory could not be created.

unzipZipFile

public static int unzipZipFile(java.util.zip.ZipFile zipFileObj,
                               java.io.File outputDir,
                               java.lang.String outputFilePrefix,
                               java.io.FileFilter filter,
                               boolean junkPathsFlag)
                        throws java.util.zip.ZipException
Unzips entries from the zip file to the specified directory.

Parameters:
zipFileObj - the zip file.
outputDir - the output directory.
outputFilePrefix - prefix to add to output filenames.
filter - The filename filter to use, or null for none.
junkPathsFlag - tue to junk paths (do not make directories), false otherwise.
Returns:
The number of files unzipped.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred or if the output directory could not be created.
See Also:
IstiFileFilter.

unzipZipFile

public static int unzipZipFile(java.util.zip.ZipFile zipFileObj,
                               java.io.File outputDir,
                               boolean keepDateFlag,
                               java.lang.String outputFilePrefix,
                               java.io.FileFilter filter,
                               boolean junkPathsFlag)
                        throws java.util.zip.ZipException
Unzips entries from the zip file to the specified directory.

Parameters:
zipFileObj - the zip file.
outputDir - the output directory.
keepDateFlag - true to retain the last-modified date/time values for the unzipped files; false to use the current time.
outputFilePrefix - prefix to add to output filenames.
filter - The filename filter to use, or null for none.
junkPathsFlag - tue to junk paths (do not make directories), false otherwise.
Returns:
The number of files unzipped.
Throws:
java.util.zip.ZipException - if a ZIP error has occurred or if the output directory could not be created.
See Also:
IstiFileFilter.

getSingleDirInZip

public static java.lang.String getSingleDirInZip(java.util.zip.ZipFile zipFileObj)
Determines if the given archive contains entries residing in a single directory and returns the directory name if so.

Parameters:
zipFileObj - archive to use.
Returns:
The name of the directory if the archive contains a single directory, an empty string if no directories were found, or null if multiple directories were found.

zipAndDeleteDir

public static java.lang.String zipAndDeleteDir(java.lang.String dirname,
                                               java.lang.String zipFile)
                                        throws java.io.FileNotFoundException,
                                               java.io.IOException
from java2s.com online example - takes a directory, and zip's it up, and then deletes the dir from the disk

Parameters:
dirname - String directory to zip up
zipFile - String the file to put the dir in. adds ".zip" if it is missing. (if file exists, we delete it first)
Returns:
String the name of the zip file. return null on error.
Throws:
java.io.FileNotFoundException
java.io.IOException

deleteDirectory

public static boolean deleteDirectory(java.io.File path)
Deletes a directory recursively.

Parameters:
path - File - the directory to delete (or file).
Returns:
boolean -true on success.

extractFileFromZip

public static boolean extractFileFromZip(java.io.File zipFileObj,
                                         java.io.File destDirFileObj,
                                         java.lang.String fileNameStr)
                                  throws java.io.IOException
Extracts a file from the given '.zip' (or '.jar') archive to the given location. The directory path of the file in the zip archive is disregarded.

Parameters:
zipFileObj - File object for source '.zip' (or '.jar') archive.
destDirFileObj - File object specifying destination directory.
fileNameStr - name of file to be extracted.
Returns:
true if successful; false if file could not be extracted.
Throws:
java.io.IOException - if an I/O error occurs.