org.zeroturnaround.zip
Class ZipUtil

java.lang.Object
  extended by org.zeroturnaround.zip.ZipUtil

public final class ZipUtil
extends Object

ZIP file manipulation utilities.

Author:
Rein Raudjärv
See Also:
containsEntry(File, String), unpackEntry(File, String), unpack(File, File), pack(File, File)

Field Summary
static int DEFAULT_COMPRESSION_LEVEL
          Default compression level
 
Method Summary
static void addEntries(File zip, ZipEntrySource[] entries, File destZip)
          Copies an existing ZIP file and appends it with new entries.
static void addEntry(File zip, String path, byte[] bytes, File destZip)
          Copies an existing ZIP file and appends it with one new entry.
static void addEntry(File zip, String path, File file, File destZip)
          Copies an existing ZIP file and appends it with one new entry.
static void addEntry(File zip, ZipEntrySource entry, File destZip)
          Copies an existing ZIP file and appends it with one new entry.
static void addOrReplaceEntries(File zip, ZipEntrySource[] entries, File destZip)
          Copies an existing ZIP file and adds/replaces the given entries in it.
static boolean archiveEquals(File f1, File f2)
          Compares two ZIP files and returns true if they contain same entries.
static void closeQuietly(ZipFile zf)
          Closes the ZIP file while ignoring any errors.
static boolean containsAnyEntry(File zip, String[] names)
          Checks if the ZIP file contains any of the given entries.
static boolean containsEntry(File zip, String name)
          Checks if the ZIP file contains the given entry.
static boolean entryEquals(File f1, File f2, String path)
          Compares same entry in two ZIP files (byte-by-byte).
static boolean entryEquals(File f1, File f2, String path1, String path2)
          Compares two ZIP entries (byte-by-byte).
static boolean entryEquals(ZipFile zf1, ZipFile zf2, String path1, String path2)
          Compares two ZIP entries (byte-by-byte).
static void explode(File zip)
          Unpacks a ZIP file to its own location.
static boolean handle(File zip, String name, ZipEntryCallback action)
          Reads the given ZIP file and executes the given action for a single entry.
static boolean handle(InputStream is, String name, ZipEntryCallback action)
          Reads the given ZIP stream and executes the given action for a single entry.
static void iterate(File zip, ZipEntryCallback action)
          Reads the given ZIP file and executes the given action for each entry.
static void iterate(File zip, ZipInfoCallback action)
          Scans the given ZIP file and executes the given action for each entry.
static void iterate(InputStream is, ZipEntryCallback action)
          Reads the given ZIP stream and executes the given action for each entry.
static void pack(File rootDir, File zip)
          Compresses the given directory and all its sub-directories into a ZIP file.
static void pack(File sourceDir, File targetZipFile, boolean preserveRoot)
          Compresses the given directory and all its sub-directories into a ZIP file.
static void pack(File rootDir, File zip, int compressionLevel)
          Compresses the given directory and all its sub-directories into a ZIP file.
static void pack(File sourceDir, File targetZip, NameMapper mapper)
          Compresses the given directory and all its sub-directories into a ZIP file.
static void pack(File sourceDir, File targetZip, NameMapper mapper, int compressionLevel)
          Compresses the given directory and all its sub-directories into a ZIP file.
static void pack(ZipEntrySource[] entries, File zip)
          Compresses the given entries into a new ZIP file.
static void packEntries(File[] filesToPack, File destZipFile)
          Compresses the given files into a ZIP file.
static byte[] packEntry(File file)
          Compresses the given file into a ZIP file with single entry.
static void packEntry(File fileToPack, File destZipFile)
          Compresses the given file into a ZIP file.
static void repack(File srcZip, File dstZip, int compressionLevel)
          Repacks a provided ZIP file into a new ZIP with a given compression level.
static void repack(File zip, int compressionLevel)
          Repacks a provided ZIP file and replaces old file with the new one.
static void repack(InputStream is, File dstZip, int compressionLevel)
          Repacks a provided ZIP input stream into a ZIP file with a given compression level.
static boolean replaceEntries(File zip, ZipEntrySource[] entries, File destZip)
          Copies an existing ZIP file and replaces the given entries in it.
static boolean replaceEntry(File zip, String path, byte[] bytes, File destZip)
          Copies an existing ZIP file and replaces a given entry in it.
static boolean replaceEntry(File zip, String path, File file, File destZip)
          Copies an existing ZIP file and replaces a given entry in it.
static boolean replaceEntry(File zip, ZipEntrySource entry, File destZip)
          Copies an existing ZIP file and replaces a given entry in it.
static boolean transformEntries(File zip, ZipEntryTransformerEntry[] entries, File destZip)
          Copies an existing ZIP file and transforms the given entries in it.
static boolean transformEntries(InputStream is, ZipEntryTransformerEntry[] entries, OutputStream os)
          Copies an existing ZIP file and transforms the given entries in it.
static boolean transformEntry(File zip, String path, ZipEntryTransformer transformer, File destZip)
          Copies an existing ZIP file and transforms a given entry in it.
static boolean transformEntry(File zip, ZipEntryTransformerEntry entry, File destZip)
          Copies an existing ZIP file and transforms a given entry in it.
static boolean transformEntry(InputStream is, String path, ZipEntryTransformer transformer, OutputStream os)
          Copies an existing ZIP file and transforms a given entry in it.
static boolean transformEntry(InputStream is, ZipEntryTransformerEntry entry, OutputStream os)
          Copies an existing ZIP file and transforms a given entry in it.
static void unexplode(File dir)
          Compresses a given directory in its own location.
static void unexplode(File dir, int compressionLevel)
          Compresses a given directory in its own location.
static void unpack(File zip, File outputDir)
          Unpacks a ZIP file to the given directory.
static void unpack(File zip, File outputDir, NameMapper mapper)
          Unpacks a ZIP file to the given directory.
static void unpack(InputStream is, File outputDir)
          Unpacks a ZIP stream to the given directory.
static void unpack(InputStream is, File outputDir, NameMapper mapper)
          Unpacks a ZIP stream to the given directory.
static byte[] unpackEntry(File zip, String name)
          Unpacks a single entry from a ZIP file.
static boolean unpackEntry(File zip, String name, File file)
          Unpacks a single file from a ZIP archive to a file.
static byte[] unpackEntry(InputStream is, String name)
          Unpacks a single entry from a ZIP stream.
static boolean unpackEntry(InputStream is, String name, File file)
          Unpacks a single file from a ZIP stream to a file.
static byte[] unpackEntry(ZipFile zf, String name)
          Unpacks a single entry from a ZIP file.
static boolean unpackEntry(ZipFile zf, String name, File file)
          Unpacks a single file from a ZIP archive to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_COMPRESSION_LEVEL

public static final int DEFAULT_COMPRESSION_LEVEL
Default compression level

See Also:
Constant Field Values
Method Detail

containsEntry

public static boolean containsEntry(File zip,
                                    String name)
Checks if the ZIP file contains the given entry.

Parameters:
zip - ZIP file.
name - entry name.
Returns:
true if the ZIP file contains the given entry.

containsAnyEntry

public static boolean containsAnyEntry(File zip,
                                       String[] names)
Checks if the ZIP file contains any of the given entries.

Parameters:
zip - ZIP file.
names - entry names.
Returns:
true if the ZIP file contains any of the given entries.

unpackEntry

public static byte[] unpackEntry(File zip,
                                 String name)
Unpacks a single entry from a ZIP file.

Parameters:
zip - ZIP file.
name - entry name.
Returns:
contents of the entry or null if it was not found.

unpackEntry

public static byte[] unpackEntry(ZipFile zf,
                                 String name)
Unpacks a single entry from a ZIP file.

Parameters:
zf - ZIP file.
name - entry name.
Returns:
contents of the entry or null if it was not found.

unpackEntry

public static byte[] unpackEntry(InputStream is,
                                 String name)
Unpacks a single entry from a ZIP stream.

Parameters:
is - ZIP stream.
name - entry name.
Returns:
contents of the entry or null if it was not found.

unpackEntry

public static boolean unpackEntry(File zip,
                                  String name,
                                  File file)
Unpacks a single file from a ZIP archive to a file.

Parameters:
zip - ZIP file.
name - entry name.
file - target file to be created or overwritten.
Returns:
true if the entry was found and unpacked, false if the entry was not found.

unpackEntry

public static boolean unpackEntry(ZipFile zf,
                                  String name,
                                  File file)
Unpacks a single file from a ZIP archive to a file.

Parameters:
zf - ZIP file.
name - entry name.
file - target file to be created or overwritten.
Returns:
true if the entry was found and unpacked, false if the entry was not found.

unpackEntry

public static boolean unpackEntry(InputStream is,
                                  String name,
                                  File file)
                           throws IOException
Unpacks a single file from a ZIP stream to a file.

Parameters:
is - ZIP stream.
name - entry name.
file - target file to be created or overwritten.
Returns:
true if the entry was found and unpacked, false if the entry was not found.
Throws:
IOException

iterate

public static void iterate(File zip,
                           ZipEntryCallback action)
Reads the given ZIP file and executes the given action for each entry.

For each entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

Parameters:
zip - input ZIP file.
action - action to be called for each entry.
See Also:
ZipEntryCallback, iterate(File, ZipInfoCallback)

iterate

public static void iterate(File zip,
                           ZipInfoCallback action)
Scans the given ZIP file and executes the given action for each entry.

Only the meta-data without the actual data is read. If you want to stop the loop then throw a ZipBreakException.

Parameters:
zip - input ZIP file.
action - action to be called for each entry.
See Also:
ZipInfoCallback, iterate(File, ZipEntryCallback)

iterate

public static void iterate(InputStream is,
                           ZipEntryCallback action)
Reads the given ZIP stream and executes the given action for each entry.

For each entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

Parameters:
is - input ZIP stream (it will not be closed automatically).
action - action to be called for each entry.
See Also:
ZipEntryCallback, iterate(File, ZipEntryCallback)

handle

public static boolean handle(File zip,
                             String name,
                             ZipEntryCallback action)
Reads the given ZIP file and executes the given action for a single entry.

Parameters:
zip - input ZIP file.
name - entry name.
action - action to be called for this entry.
Returns:
true if the entry was found, false if the entry was not found.
See Also:
ZipEntryCallback

handle

public static boolean handle(InputStream is,
                             String name,
                             ZipEntryCallback action)
Reads the given ZIP stream and executes the given action for a single entry.

Parameters:
is - input ZIP stream (it will not be closed automatically).
name - entry name.
action - action to be called for this entry.
Returns:
true if the entry was found, false if the entry was not found.
See Also:
ZipEntryCallback

unpack

public static void unpack(File zip,
                          File outputDir)
Unpacks a ZIP file to the given directory.

The output directory must not be a file.

Parameters:
zip - input ZIP file.
outputDir - output directory (created automatically if not found).

unpack

public static void unpack(File zip,
                          File outputDir,
                          NameMapper mapper)
Unpacks a ZIP file to the given directory.

The output directory must not be a file.

Parameters:
zip - input ZIP file.
outputDir - output directory (created automatically if not found).

unpack

public static void unpack(InputStream is,
                          File outputDir)
Unpacks a ZIP stream to the given directory.

The output directory must not be a file.

Parameters:
zip - input ZIP file.
outputDir - output directory (created automatically if not found).

unpack

public static void unpack(InputStream is,
                          File outputDir,
                          NameMapper mapper)
Unpacks a ZIP stream to the given directory.

The output directory must not be a file.

Parameters:
zip - input ZIP file.
outputDir - output directory (created automatically if not found).

explode

public static void explode(File zip)
Unpacks a ZIP file to its own location.

The ZIP file will be first renamed (using a temporary name). After the extraction it will be deleted.

Parameters:
zip - input ZIP file as well as the target directory.
See Also:
unpack(File, File)

packEntry

public static byte[] packEntry(File file)
Compresses the given file into a ZIP file with single entry.

Parameters:
file - file to be compressed.
Returns:
ZIP file created.

pack

public static void pack(File rootDir,
                        File zip)
Compresses the given directory and all its sub-directories into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist. Will not include the root directory name in the archive.

Parameters:
File - root directory.
zip - ZIP file that will be created or overwritten.

pack

public static void pack(File rootDir,
                        File zip,
                        int compressionLevel)
Compresses the given directory and all its sub-directories into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist. Will not include the root directory name in the archive.

Parameters:
File - root directory.
zip - ZIP file that will be created or overwritten.
compressionLevel - compression level

pack

public static void pack(File sourceDir,
                        File targetZipFile,
                        boolean preserveRoot)
Compresses the given directory and all its sub-directories into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist. Will not include the root directory name in the archive.

Parameters:
sourceDir - root directory.
targetZipFile - ZIP file that will be created or overwritten.

packEntry

public static void packEntry(File fileToPack,
                             File destZipFile)
Compresses the given file into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist.

Parameters:
File - file that needs to be zipped.
destZipFile - ZIP file that will be created or overwritten.

packEntries

public static void packEntries(File[] filesToPack,
                               File destZipFile)
Compresses the given files into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist.

Parameters:
File - files that needs to be zipped.
destZipFile - ZIP file that will be created or overwritten.

pack

public static void pack(File sourceDir,
                        File targetZip,
                        NameMapper mapper)
Compresses the given directory and all its sub-directories into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist.

Parameters:
sourceDir - root directory.
targetZip - ZIP file that will be created or overwritten.

pack

public static void pack(File sourceDir,
                        File targetZip,
                        NameMapper mapper,
                        int compressionLevel)
Compresses the given directory and all its sub-directories into a ZIP file.

The ZIP file must not be a directory and its parent directory must exist.

Parameters:
sourceDir - root directory.
targetZip - ZIP file that will be created or overwritten.
compressionLevel - compression level

repack

public static void repack(File srcZip,
                          File dstZip,
                          int compressionLevel)
Repacks a provided ZIP file into a new ZIP with a given compression level.

Parameters:
srcZip - source ZIP file.
dstZip - destination ZIP file.
compressionLevel - compression level.

repack

public static void repack(InputStream is,
                          File dstZip,
                          int compressionLevel)
Repacks a provided ZIP input stream into a ZIP file with a given compression level.

Parameters:
is - ZIP input stream.
dstZip - destination ZIP file.
compressionLevel - compression level.

repack

public static void repack(File zip,
                          int compressionLevel)
Repacks a provided ZIP file and replaces old file with the new one.

Parameters:
zip - source ZIP file to be repacked and replaced.
compressionLevel - compression level.

unexplode

public static void unexplode(File dir)
Compresses a given directory in its own location.

A ZIP file will be first created with a temporary name. After the compressing the directory will be deleted and the ZIP file will be renamed as the original directory.

Parameters:
dir - input directory as well as the target ZIP file.
See Also:
pack(File, File)

unexplode

public static void unexplode(File dir,
                             int compressionLevel)
Compresses a given directory in its own location.

A ZIP file will be first created with a temporary name. After the compressing the directory will be deleted and the ZIP file will be renamed as the original directory.

Parameters:
dir - input directory as well as the target ZIP file.
compressionLevel - compression level
See Also:
pack(File, File)

pack

public static void pack(ZipEntrySource[] entries,
                        File zip)
Compresses the given entries into a new ZIP file.

Parameters:
entries - ZIP entries added.
zip - new ZIP file created.

addEntry

public static void addEntry(File zip,
                            String path,
                            File file,
                            File destZip)
Copies an existing ZIP file and appends it with one new entry.

Parameters:
zip - an existing ZIP file (only read).
path - new ZIP entry path.
file - new entry to be added.
destZip - new ZIP file created.

addEntry

public static void addEntry(File zip,
                            String path,
                            byte[] bytes,
                            File destZip)
Copies an existing ZIP file and appends it with one new entry.

Parameters:
zip - an existing ZIP file (only read).
path - new ZIP entry path.
bytes - new entry bytes (or null if directory).
destZip - new ZIP file created.

addEntry

public static void addEntry(File zip,
                            ZipEntrySource entry,
                            File destZip)
Copies an existing ZIP file and appends it with one new entry.

Parameters:
zip - an existing ZIP file (only read).
entry - new ZIP entry appended.
destZip - new ZIP file created.

addEntries

public static void addEntries(File zip,
                              ZipEntrySource[] entries,
                              File destZip)
Copies an existing ZIP file and appends it with new entries.

Parameters:
zip - an existing ZIP file (only read).
entries - new ZIP entries appended.
destZip - new ZIP file created.

replaceEntry

public static boolean replaceEntry(File zip,
                                   String path,
                                   File file,
                                   File destZip)
Copies an existing ZIP file and replaces a given entry in it.

Parameters:
zip - an existing ZIP file (only read).
path - new ZIP entry path.
file - new entry.
destZip - new ZIP file created.
Returns:
true if the entry was replaced.

replaceEntry

public static boolean replaceEntry(File zip,
                                   String path,
                                   byte[] bytes,
                                   File destZip)
Copies an existing ZIP file and replaces a given entry in it.

Parameters:
zip - an existing ZIP file (only read).
path - new ZIP entry path.
bytes - new entry bytes (or null if directory).
destZip - new ZIP file created.
Returns:
true if the entry was replaced.

replaceEntry

public static boolean replaceEntry(File zip,
                                   ZipEntrySource entry,
                                   File destZip)
Copies an existing ZIP file and replaces a given entry in it.

Parameters:
zip - an existing ZIP file (only read).
entry - new ZIP entry.
destZip - new ZIP file created.
Returns:
true if the entry was replaced.

replaceEntries

public static boolean replaceEntries(File zip,
                                     ZipEntrySource[] entries,
                                     File destZip)
Copies an existing ZIP file and replaces the given entries in it.

Parameters:
zip - an existing ZIP file (only read).
entries - new ZIP entries to be replaced with.
destZip - new ZIP file created.
Returns:
true if at least one entry was replaced.

addOrReplaceEntries

public static void addOrReplaceEntries(File zip,
                                       ZipEntrySource[] entries,
                                       File destZip)
Copies an existing ZIP file and adds/replaces the given entries in it.

Parameters:
zip - an existing ZIP file (only read).
entries - ZIP entries to be replaced or added.
destZip - new ZIP file created.

transformEntry

public static boolean transformEntry(File zip,
                                     String path,
                                     ZipEntryTransformer transformer,
                                     File destZip)
Copies an existing ZIP file and transforms a given entry in it.

Parameters:
zip - an existing ZIP file (only read).
path - new ZIP entry path.
transformer - transformer for the given ZIP entry.
destZip - new ZIP file created.
Returns:
true if the entry was replaced.

transformEntry

public static boolean transformEntry(File zip,
                                     ZipEntryTransformerEntry entry,
                                     File destZip)
Copies an existing ZIP file and transforms a given entry in it.

Parameters:
zip - an existing ZIP file (only read).
entry - transformer for a ZIP entry.
destZip - new ZIP file created.
Returns:
true if the entry was replaced.

transformEntries

public static boolean transformEntries(File zip,
                                       ZipEntryTransformerEntry[] entries,
                                       File destZip)
Copies an existing ZIP file and transforms the given entries in it.

Parameters:
zip - an existing ZIP file (only read).
entries - ZIP entry transformers.
destZip - new ZIP file created.
Returns:
true if at least one entry was replaced.

transformEntry

public static boolean transformEntry(InputStream is,
                                     String path,
                                     ZipEntryTransformer transformer,
                                     OutputStream os)
Copies an existing ZIP file and transforms a given entry in it.

Parameters:
is - a ZIP input stream.
path - new ZIP entry path.
transformer - transformer for the given ZIP entry.
os - a ZIP output stream.
Returns:
true if the entry was replaced.

transformEntry

public static boolean transformEntry(InputStream is,
                                     ZipEntryTransformerEntry entry,
                                     OutputStream os)
Copies an existing ZIP file and transforms a given entry in it.

Parameters:
is - a ZIP input stream.
entry - transformer for a ZIP entry.
os - a ZIP output stream.
Returns:
true if the entry was replaced.

transformEntries

public static boolean transformEntries(InputStream is,
                                       ZipEntryTransformerEntry[] entries,
                                       OutputStream os)
Copies an existing ZIP file and transforms the given entries in it.

Parameters:
is - a ZIP input stream.
entries - ZIP entry transformers.
os - a ZIP output stream.
Returns:
true if at least one entry was replaced.

archiveEquals

public static boolean archiveEquals(File f1,
                                    File f2)
Compares two ZIP files and returns true if they contain same entries.

First the two files are compared byte-by-byte. If a difference is found the corresponding entries of both ZIP files are compared. Thus if same contents is packed differently the two archives may still be the same.

Two archives are considered the same if

  1. they contain same number of entries,
  2. for each entry in the first archive there exists an entry with the same in the second archive
  3. for each entry in the first archive and the entry with the same name in the second archive
    1. both are either directories or files,
    2. both have the same size,
    3. both have the same CRC,
    4. both have the same contents (compared byte-by-byte).

Parameters:
f1 - first ZIP file.
f2 - second ZIP file.
Returns:
true if the two ZIP files contain same entries, false if a difference was found or an error occurred during the comparison.

entryEquals

public static boolean entryEquals(File f1,
                                  File f2,
                                  String path)
Compares same entry in two ZIP files (byte-by-byte).

Parameters:
f1 - first ZIP file.
f2 - second ZIP file.
path - name of the entry.
Returns:
true if the contents of the entry was same in both ZIP files.

entryEquals

public static boolean entryEquals(File f1,
                                  File f2,
                                  String path1,
                                  String path2)
Compares two ZIP entries (byte-by-byte). .

Parameters:
f1 - first ZIP file.
f2 - second ZIP file.
path1 - name of the first entry.
path2 - name of the second entry.
Returns:
true if the contents of the entries were same.

entryEquals

public static boolean entryEquals(ZipFile zf1,
                                  ZipFile zf2,
                                  String path1,
                                  String path2)
Compares two ZIP entries (byte-by-byte). .

Parameters:
zf1 - first ZIP file.
zf2 - second ZIP file.
path1 - name of the first entry.
path2 - name of the second entry.
Returns:
true if the contents of the entries were same.

closeQuietly

public static void closeQuietly(ZipFile zf)
Closes the ZIP file while ignoring any errors.

Parameters:
zf - ZIP file to be closed.


Copyright © 2013 ZeroTurnaround. All Rights Reserved.