net.livesite.util
Class JavaArchive

java.lang.Object
  extended by java.util.zip.ZipFile
      extended by java.util.jar.JarFile
          extended by net.livesite.util.JavaArchive

public class JavaArchive
extends JarFile

Using the JAR file utilities, this class provides a simple interface to access files in an indexed JAR file. The term indexed means the archive is structred like so:

Content entries: The java program will use the key to extract files from the archive:
  JavaArchive jar = new JavaArchive("/path/to/archive.jar");
  jar.extract("key", "/destination/directory");
 
The content value(s) listed for "key" will be extracted to the destination directory. In the case where the value is a directory, determining whether the directory is extracted as a destination subdirectory, or the contents are extracted is done by the syntax of the content value. For example, the content entry:
  key: images/; clipart/
 
Would create /destination/directory/images and /destination/directory/clipart. However, the content entry:
  key: images/*; clipart/*
 
Will extract the JAR contents of images and clipart to /destination/directory.

Usage example:

An archive intended to install the packman game has this listing:
      META-INF/
      META-INF/MANIFEST.MF
      bin/
      bin/client
      bin/server
      man/
      bin/client/packman
      bin/server/packmand
      man/packman.1
      man/packmand.1
 
The manifest will have:
      Manifest-Version: 1.0
      Jar-Type: application/installer
      binaries: bin/client; bin/server
      documentation: man
 
Letting one write the code (which does not directly depend on the archive's file structure):
      JavaArchive jar = new JavaArchive("packman.jar","application/installer");
      jar.extractDir("binaries", "/usr/bin");
      jar.extractDir("documentation", "/usr/man/man1");
 

Notes:

Author:
Ryan Gies
See Also:
Manifest, JarFile

Nested Class Summary
 class JavaArchive.AttributeNotFoundException
          Thrown when an attribute was not found in the manifest.
 class JavaArchive.CannotCreateException
          Thrown when the jar type is not that which was requested.
 class JavaArchive.ExtractEntryFileException
          Thrown when we have a problem with extracting the entry.
 class JavaArchive.FileIOException
          Thrown when we try to do file I/O.
 class JavaArchive.FileTypeMismatchException
          Thrown when we try to copy a directory into a file.
 class JavaArchive.IncorrectJarTypeException
          Thrown when the jar type is not that which was requested.
 
Field Summary
static int CENATT
           
static int CENATX
           
static int CENCOM
           
static int CENCRC
           
static int CENDSK
           
static int CENEXT
           
static int CENFLG
           
static int CENHDR
           
static int CENHOW
           
static int CENLEN
           
static int CENNAM
           
static int CENOFF
           
static long CENSIG
           
static int CENSIZ
           
static int CENTIM
           
static int CENVEM
           
static int CENVER
           
static int ENDCOM
           
static int ENDHDR
           
static int ENDOFF
           
static long ENDSIG
           
static int ENDSIZ
           
static int ENDSUB
           
static int ENDTOT
           
static int EXTCRC
           
static int EXTHDR
           
static int EXTLEN
           
static long EXTSIG
           
static int EXTSIZ
           
protected static String JAR_TYPE_KEY
           
static int LOCCRC
           
static int LOCEXT
           
static int LOCFLG
           
static int LOCHDR
           
static int LOCHOW
           
static int LOCLEN
           
static int LOCNAM
           
static long LOCSIG
           
static int LOCSIZ
           
static int LOCTIM
           
static int LOCVER
           
 
Fields inherited from class java.util.jar.JarFile
MANIFEST_NAME
 
Fields inherited from class java.util.zip.ZipFile
OPEN_DELETE, OPEN_READ
 
Constructor Summary
JavaArchive(File file)
          Constructor.
JavaArchive(File file, String type)
          Constructor.
JavaArchive(File file, String type, boolean verify)
          Constructor.
JavaArchive(File file, String type, boolean verify, int mode)
          Constructor.
JavaArchive(String file)
          Constructor.
JavaArchive(String name, String type)
          Constructor.
JavaArchive(String name, String type, boolean verify)
          Constructor.
 
Method Summary
 List extract(String key, File dest)
          Extract entries from the archive.
 List extract(String key, String dest)
          Extract entries from the archive.
 String getAttribute(String key)
          Get an attribute from the main manifest.
static String getJarType(File file)
          Get the jar-type value from the given jar file.
static String getJarType(String filename)
          Get the jar-type value from the given jar file.
 void validateType(String requiredType)
          Ensure the archive is of the required type.
 
Methods inherited from class java.util.jar.JarFile
entries, getEntry, getInputStream, getJarEntry, getManifest
 
Methods inherited from class java.util.zip.ZipFile
close, finalize, getName, size
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAR_TYPE_KEY

protected static final String JAR_TYPE_KEY
See Also:
Constant Field Values

LOCSIG

public static final long LOCSIG
See Also:
Constant Field Values

EXTSIG

public static final long EXTSIG
See Also:
Constant Field Values

CENSIG

public static final long CENSIG
See Also:
Constant Field Values

ENDSIG

public static final long ENDSIG
See Also:
Constant Field Values

LOCHDR

public static final int LOCHDR
See Also:
Constant Field Values

EXTHDR

public static final int EXTHDR
See Also:
Constant Field Values

CENHDR

public static final int CENHDR
See Also:
Constant Field Values

ENDHDR

public static final int ENDHDR
See Also:
Constant Field Values

LOCVER

public static final int LOCVER
See Also:
Constant Field Values

LOCFLG

public static final int LOCFLG
See Also:
Constant Field Values

LOCHOW

public static final int LOCHOW
See Also:
Constant Field Values

LOCTIM

public static final int LOCTIM
See Also:
Constant Field Values

LOCCRC

public static final int LOCCRC
See Also:
Constant Field Values

LOCSIZ

public static final int LOCSIZ
See Also:
Constant Field Values

LOCLEN

public static final int LOCLEN
See Also:
Constant Field Values

LOCNAM

public static final int LOCNAM
See Also:
Constant Field Values

LOCEXT

public static final int LOCEXT
See Also:
Constant Field Values

EXTCRC

public static final int EXTCRC
See Also:
Constant Field Values

EXTSIZ

public static final int EXTSIZ
See Also:
Constant Field Values

EXTLEN

public static final int EXTLEN
See Also:
Constant Field Values

CENVEM

public static final int CENVEM
See Also:
Constant Field Values

CENVER

public static final int CENVER
See Also:
Constant Field Values

CENFLG

public static final int CENFLG
See Also:
Constant Field Values

CENHOW

public static final int CENHOW
See Also:
Constant Field Values

CENTIM

public static final int CENTIM
See Also:
Constant Field Values

CENCRC

public static final int CENCRC
See Also:
Constant Field Values

CENSIZ

public static final int CENSIZ
See Also:
Constant Field Values

CENLEN

public static final int CENLEN
See Also:
Constant Field Values

CENNAM

public static final int CENNAM
See Also:
Constant Field Values

CENEXT

public static final int CENEXT
See Also:
Constant Field Values

CENCOM

public static final int CENCOM
See Also:
Constant Field Values

CENDSK

public static final int CENDSK
See Also:
Constant Field Values

CENATT

public static final int CENATT
See Also:
Constant Field Values

CENATX

public static final int CENATX
See Also:
Constant Field Values

CENOFF

public static final int CENOFF
See Also:
Constant Field Values

ENDSUB

public static final int ENDSUB
See Also:
Constant Field Values

ENDTOT

public static final int ENDTOT
See Also:
Constant Field Values

ENDSIZ

public static final int ENDSIZ
See Also:
Constant Field Values

ENDOFF

public static final int ENDOFF
See Also:
Constant Field Values

ENDCOM

public static final int ENDCOM
See Also:
Constant Field Values
Constructor Detail

JavaArchive

public JavaArchive(String file)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
file - the JAR file to be opened for reading
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file

JavaArchive

public JavaArchive(File file)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
file - the JAR file to be opened for reading
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file

JavaArchive

public JavaArchive(File file,
                   String type)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
file - the JAR file to be opened for reading
type - the required Jar-Type (or "*" for any type)
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file
See Also:
JarFile.JarFile(File)

JavaArchive

public JavaArchive(File file,
                   String type,
                   boolean verify)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
file - the JAR file to be opened for reading
type - the required Jar-Type (or "*" for any type)
verify - whether or not to verify the JarFile if it is signed SecurityManager
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file
See Also:
JarFile.JarFile(File, boolean)

JavaArchive

public JavaArchive(String name,
                   String type)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
name - the name of the JAR file to be opened for reading
type - the required Jar-Type (or "*" for any type)
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file
See Also:
JarFile.JarFile(String)

JavaArchive

public JavaArchive(String name,
                   String type,
                   boolean verify)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
name - the name of the JAR file to be opened for reading
type - the required Jar-Type (or "*" for any type)
verify - whether or not to verify the JarFile if it is signed
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file

JavaArchive

public JavaArchive(File file,
                   String type,
                   boolean verify,
                   int mode)
            throws IOException,
                   JavaArchive.AttributeNotFoundException,
                   JavaArchive.IncorrectJarTypeException
Constructor.

Parameters:
file - the JAR file to be opened
type - the required Jar-Type (or "*" for any type)
verify - whether or not to verify the JarFile if it is signed
mode - the mode in which the file is to be opened
Throws:
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match
IOException - unable to open file
Method Detail

getJarType

public static String getJarType(String filename)
                         throws IOException
Get the jar-type value from the given jar file.

Parameters:
filename - path to jar file
Returns:
the 'jar-type' attribute value in the mainifest file
Throws:
IOException - unable to open file
See Also:
getJarType(File)

getJarType

public static String getJarType(File file)
                         throws IOException
Get the jar-type value from the given jar file.

Parameters:
file - file object
Returns:
the 'jar-type' attribute value in the manifest file
Throws:
IOException - unable to open the archive file

validateType

public void validateType(String requiredType)
                  throws IOException,
                         JavaArchive.AttributeNotFoundException,
                         JavaArchive.IncorrectJarTypeException
Ensure the archive is of the required type.

Parameters:
requiredType - required jar type (or "*" for any type)
Throws:
IOException - when an I/O error occurs
JavaArchive.AttributeNotFoundException - when there is no Jar-Type defined
JavaArchive.IncorrectJarTypeException - when the Jar-Type does not match

getAttribute

public String getAttribute(String key)
                    throws IOException
Get an attribute from the main manifest.

Parameters:
key - the attribute key
Returns:
the attribute value
Throws:
IOException - unable to open archive file

extract

public List extract(String key,
                    String dest)
             throws Exception
Extract entries from the archive.

Parameters:
key - the attribute key (specified in the manifest)
dest - the destination directory
Returns:
List of extracted names
Throws:
JavaArchive.IncorrectJarTypeException - general archive exception
Exception
See Also:
extract(String, File)

extract

public List extract(String key,
                    File dest)
             throws IOException,
                    JavaArchive.AttributeNotFoundException,
                    JavaArchive.ExtractEntryFileException
Extract entries from the archive.

Parameters:
key - the attribute key (specified in the manifest)
dest - the destination directory
Returns:
List of extracted names
Throws:
IOException - problem with archive file
JavaArchive.AttributeNotFoundException - attribute key is not in the manifest
JavaArchive.ExtractEntryFileException - problem extracting an individual entry
See Also:
extract(String, File)