public class ZipFileUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String[] |
FILE_EXTENSIONS
Zip file extensions (in lower case.)
|
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(java.util.zip.ZipFile zipFile)
Close quietly ignoring any exceptions.
|
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
|
public static java.lang.String[] FILE_EXTENSIONS
public static void closeQuietly(java.util.zip.ZipFile zipFile)
zipFile
- the ZIP file to be closed or null if none.public static boolean hasZipExtension(java.lang.String name)
name
- the name of the zip filepublic static java.util.zip.ZipFile openZipFile(java.lang.String name) throws java.io.IOException
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.
name
- the name of the zip filejava.util.zip.ZipException
- if a ZIP format error has occurredjava.io.IOException
- if an I/O error has occurredjava.lang.SecurityException
- if a security manager exists and its
checkRead
method doesn't allow read access to the file.SecurityManager.checkRead(java.lang.String)
public static java.util.zip.ZipFile openZipFile(java.io.File file, int mode) throws java.io.IOException
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.
file
- the ZIP file to be opened for readingmode
- the mode in which the file is to be openedjava.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.SecurityManager.checkRead(java.lang.String)
public static java.util.zip.ZipFile openZipFile(java.io.File file) throws java.util.zip.ZipException, java.io.IOException
file
- the ZIP file to be opened for readingjava.util.zip.ZipException
- if a ZIP error has occurred.java.io.IOException
- if an I/O error has occurred.public static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir) throws java.util.zip.ZipException
zipFileObj
- the zip file.outputDir
- the output directory.java.util.zip.ZipException
- if a ZIP error has occurred or if the
output directory could not be created.public static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, boolean keepDateFlag) throws java.util.zip.ZipException
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.java.util.zip.ZipException
- if a ZIP error has occurred or if the
output directory could not be created.public static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, java.lang.String outputFilePrefix) throws java.util.zip.ZipException
zipFileObj
- the zip file.outputDir
- the output directory.outputFilePrefix
- prefix to add to output filenames.java.util.zip.ZipException
- if a ZIP error has occurred or if the
output directory could not be created.public static int unzipZipFile(java.util.zip.ZipFile zipFileObj, java.io.File outputDir, boolean keepDateFlag, java.lang.String outputFilePrefix) throws java.util.zip.ZipException
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.java.util.zip.ZipException
- if a ZIP error has occurred or if the
output directory could not be created.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
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.java.util.zip.ZipException
- if a ZIP error has occurred or if the
output directory could not be created.IstiFileFilter.
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
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.java.util.zip.ZipException
- if a ZIP error has occurred or if the
output directory could not be created.IstiFileFilter.
public static java.lang.String getSingleDirInZip(java.util.zip.ZipFile zipFileObj)
zipFileObj
- archive to use.public static java.lang.String zipAndDeleteDir(java.lang.String dirname, java.lang.String zipFile) throws java.io.FileNotFoundException, java.io.IOException
dirname
- String directory to zip upzipFile
- String the file to put the dir in. adds ".zip" if it is missing. (if file exists, we delete it first)java.io.FileNotFoundException
java.io.IOException
public static boolean extractFileFromZip(java.io.File zipFileObj, java.io.File destDirFileObj, java.lang.String fileNameStr) throws java.io.IOException
zipFileObj
- File object for source '.zip' (or '.jar') archive.destDirFileObj
- File object specifying destination directory.fileNameStr
- name of file to be extracted.java.io.IOException
- if an I/O error occurs.