com.springsource.util.io
Class ZipUtils

java.lang.Object
  extended by com.springsource.util.io.ZipUtils

public class ZipUtils
extends java.lang.Object

Utility code for working with Zip files.

Concurrent Semantics
Thread-safe.

Since:
Jersey

Field Summary
private static int BUFFER_SIZE
           
private static java.lang.String ZIP_FILENAME_SUFFIX
           
 
Constructor Summary
ZipUtils()
           
 
Method Summary
private static PathReference determineFinalUnzipDestination(PathReference zipFile, PathReference dest)
           
private static PathReference determineFinalZipDestination(PathReference toZip, PathReference dest)
           
private static java.lang.String determineNameOfEntry(java.io.File file, java.io.File root, java.lang.String entryPrefix)
           
private static void doZip(java.util.zip.ZipOutputStream zos, java.io.File file, java.io.File root, java.lang.String entryPrefix)
           
static PathReference unzipTo(PathReference zipFile, PathReference dest)
          Unzips the Zip file at jarFile to the directory dest.

If the supplied dest PathReference does not exist, it is created as a directory and the Zip is unzipped directly into the newly created directory.

If the supplied dest PathReference already exists and is a directory then the Zip is unzipped as a subdirectory of the supplied directory.

static PathReference zipTo(PathReference toZip, PathReference dest)
          Zips the file or directory at toZip and writes the resulting Zip file to the supplied destination.
static PathReference zipTo(PathReference toZip, PathReference dest, java.lang.String entryPrefix)
          Zips the file or directory at toZip and writes the resulting Zip file to the supplied directory, dest.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZIP_FILENAME_SUFFIX

private static final java.lang.String ZIP_FILENAME_SUFFIX
See Also:
Constant Field Values

BUFFER_SIZE

private static final int BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

ZipUtils

public ZipUtils()
Method Detail

unzipTo

public static PathReference unzipTo(PathReference zipFile,
                                    PathReference dest)
                             throws java.io.IOException
Unzips the Zip file at jarFile to the directory dest.

If the supplied dest PathReference does not exist, it is created as a directory and the Zip is unzipped directly into the newly created directory.

If the supplied dest PathReference already exists and is a directory then the Zip is unzipped as a subdirectory of the supplied directory. The name of the generated subdirectory is that of the Zip file without the file extension, e.g. foo.zip is unpacked into a directory foo.

Parameters:
zipFile - the Zip file to unzip
dest - the destination directory
Returns:
a PathReference to the directory containing the Zip file's contents.
Throws:
java.io.IOException - if an error occurs during unzip.

zipTo

public static PathReference zipTo(PathReference toZip,
                                  PathReference dest)
                           throws java.io.IOException
Zips the file or directory at toZip and writes the resulting Zip file to the supplied destination.

If the supplied destination does not exist the resulting Zip file will be written to that destination. If the supplied destination exists, and it is a directory, the Zip file will be written to that directory with the name as the path to be zipped, suffixed with ".zip". If the supplied destination exists, and it is not a directory, and exception is thrown.

The entries in the resulting zip file are named relative to the path toZip.

Parameters:
toZip - The file or directory from which the Zip file is to be created.
dest - The path to which the created Zip file is to be written.
Returns:
a PathReference to the created Zip file.
Throws:
java.io.IOException - if an error occurs during the zip process.

zipTo

public static PathReference zipTo(PathReference toZip,
                                  PathReference dest,
                                  java.lang.String entryPrefix)
                           throws java.io.IOException
Zips the file or directory at toZip and writes the resulting Zip file to the supplied directory, dest.

If the supplied destination does not exist the resulting Zip file will be written to that destination. If the supplied destination exists, and it is a directory, the Zip file will be written to that directory with the name as the path to be zipped, suffixed with ".zip". If the supplied destination exists, and it is not a directory, and exception is thrown.

Entries in the resulting zip file are named relative to the path toZip. Each entry's name is prefixed with the supplied entryPrefix.

Parameters:
toZip - The file or directory from which the Zip file is to be created.
dest - The path to which the created Zip file is to be written.
entryPrefix - for all entry names, null means no prefix
Returns:
final Zip destination PathReference
Throws:
java.io.IOException - if an error occurs during the zip process.

doZip

private static void doZip(java.util.zip.ZipOutputStream zos,
                          java.io.File file,
                          java.io.File root,
                          java.lang.String entryPrefix)
                   throws java.io.IOException
Throws:
java.io.IOException

determineNameOfEntry

private static java.lang.String determineNameOfEntry(java.io.File file,
                                                     java.io.File root,
                                                     java.lang.String entryPrefix)

determineFinalUnzipDestination

private static PathReference determineFinalUnzipDestination(PathReference zipFile,
                                                            PathReference dest)

determineFinalZipDestination

private static PathReference determineFinalZipDestination(PathReference toZip,
                                                          PathReference dest)