Class MemoryJarFile

All Implemented Interfaces:
IJarFile, Closeable, AutoCloseable

public class MemoryJarFile extends JarFile implements IJarFile
Extends the normal Jar file for processing files with various character case, and mixture of forward and backslashes. It is used to load entirely into memory and supports decryption on entry basis.

Please note that this class currently only supports file items smaller than 2 GB, i.e. a 31 bit long positive integer value.

Author:
Christopher Mindus
  • Field Details

  • Method Details

    • createJarFile

      public static MemoryJarFile createJarFile(JarInputStream jin, long lastModified) throws IOException
      Creates a memory Jar file. The Jar input stream is closed before returning, the new Jar file will not have the 'winStyle' parameter set.
      Parameters:
      jin - the Jar input stream.
      lastModified - time for last file modification, -1L for when the first empty Jar was created.
      Throws:
      IOException - for file failures.
    • createJarFile

      public static MemoryJarFile createJarFile(JarInputStream jin, boolean winStyle, long lastModified) throws IOException
      Creates a memory Jar file. The Jar input stream is closed before returning.
      Parameters:
      jin - the Jar input stream.
      winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
      lastModified - time for last file modification, -1L for when the first empty Jar was created.
      Throws:
      IOException - for file failures.
    • createJarFile

      public static MemoryJarFile createJarFile(JarInputStream jin, boolean winStyle, MemoryJarFileDecrypter decrypter, long lastModified) throws IOException
      Creates a memory Jar file. The Jar input stream is closed before returning.
      Parameters:
      jin - the Jar input stream.
      winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
      decrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of an entry.
      lastModified - time for last file modification, -1L for when the first empty Jar was created.
      Throws:
      IOException - for file failures.
    • createJarFile

      public static MemoryJarFile createJarFile(InputStream in, boolean verify, boolean winStyle, MemoryJarFileDecrypter streamDecrypter, MemoryJarFileDecrypter entryDecrypter, long lastModified) throws IOException
      Creates a memory Jar file. The input stream is closed before returning.
      Parameters:
      in - an input stream.
      verify - set to true to verify the Jar file for correctness.
      winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
      streamDecrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of the entire input stream data, set to null for no decryption.
      entryDecrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of an entry, set to null for no decryption.
      lastModified - time for last file modification, -1L for when the first empty Jar was created.
      Throws:
      IOException - for file failures.
    • createJarFile

      public static MemoryJarFile createJarFile(String fileName, boolean verify, boolean winStyle, MemoryJarFileDecrypter streamDecrypter, MemoryJarFileDecrypter entryDecrypter) throws IOException
      Creates a memory Jar file. The file is closed before returning.
      Parameters:
      fileName - the file name of Jar file to read.
      verify - set to true to verify the Jar file for correctness.
      winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
      streamDecrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of the entire input stream data, set to null for no decryption.
      entryDecrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of an entry, set to null for no decryption.
      Throws:
      IOException - for file failures.
    • createJarFile

      public static MemoryJarFile createJarFile(File file, boolean verify, boolean winStyle, MemoryJarFileDecrypter streamDecrypter, MemoryJarFileDecrypter entryDecrypter) throws IOException
      Creates a memory Jar file. The File is closed before returning.
      Parameters:
      file - the Jar File instance to read.
      verify - set to true to verify the Jar file for correctness.
      winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
      streamDecrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of the entire input stream data, set to null for no decryption.
      entryDecrypter - an instance of a class implementing the MemoryJarFileDecrypter interface to provide decryption of an entry, set to null for no decryption.
      Throws:
      IOException - for file failures.
    • getJarFile

      public File getJarFile()
      Gets the file of the Jar file.
      Specified by:
      getJarFile in interface IJarFile
      Returns:
      Always returns null as there is no File representation for a memory Jar.
    • lastModified

      public long lastModified()
      Returns the time that the file denoted by this abstract pathname was last modified.
      Specified by:
      lastModified in interface IJarFile
      Returns:
      A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs
    • getHandler

      public Handler getHandler()
      Gets the URL stream handler for the MemoryJarFile.
      Specified by:
      getHandler in interface IJarFile
      Returns:
      The stream handler.
    • getURL

      public URL getURL()
      Gets the MemoryJar URL to be used with e.g. a class loader.
      Specified by:
      getURL in interface IJarFile
      Returns:
      An URL with the stream handler initialized to this Jar file.
    • getURL

      public URL getURL(ZipEntry entry)
      Gets the MemoryJar URL to be used with e.g. a class loader.
      Specified by:
      getURL in interface IJarFile
      Parameters:
      entry - The Jar entry.
      Returns:
      An URL with the stream handler initialized to this Jar file.
    • getURL

      public URL getURL(String path)
      Gets the MemoryJar URL to be used with e.g. a class loader.
      Specified by:
      getURL in interface IJarFile
      Parameters:
      path - A path specification.
      Returns:
      An URL with the stream handler initialized to this Jar file.
    • entries

      public Enumeration<JarEntry> entries()
      Returns an enumeration of the Jar file entries.
      Specified by:
      entries in interface IJarFile
      Overrides:
      entries in class JarFile
      Returns:
      an enumeration of the Jar file entries.
    • stream

      public Stream<JarEntry> stream()
      Returns a stream of Jar entries.
      Specified by:
      stream in interface IJarFile
      Overrides:
      stream in class JarFile
      Returns:
      an ordered, distinct, immutable and non-null stream of the Jar file entries.
    • getJarEntries

      public JarEntry[] getJarEntries()
      Gets all Jar entries if the Jar file is loaded into memory.
      Specified by:
      getJarEntries in interface IJarFile
      Returns:
      All the entries as a new array.
    • getInputStream

      public InputStream getInputStream(ZipEntry ze) throws FileNotFoundException
      Returns an input stream for reading the contents of the specified zip file entry.
      Specified by:
      getInputStream in interface IJarFile
      Overrides:
      getInputStream in class JarFile
      Parameters:
      ze - The Zip or Jar file entry.
      Returns:
      an input stream for reading the contents of the specified zip file entry
      Throws:
      FileNotFoundException - if the entry isn't found.
    • getByteArray

      public byte[] getByteArray(ZipEntry ze) throws FileNotFoundException
      Returns the byte array of the contents of the specified zip file entry.
      Returns:
      the byte array of the contents of the specified zip file entry
      Throws:
      FileNotFoundException - if the entry isn't found.
    • getJarEntry

      public JarEntry getJarEntry(String name)
      Gets the Jar entry with the specified name.
      Specified by:
      getJarEntry in interface IJarFile
      Overrides:
      getJarEntry in class JarFile
      Parameters:
      name - name of the Jar entry, 'winStyle' parameter applies, i.e. '\' is translated to '/' and the name check is done case insensitive.
      Returns:
      the JarEntry found, or null for not found.
      See Also:
    • getEntry

      public ZipEntry getEntry(String name)
      Gets the Zip entry with the specified name.
      Specified by:
      getEntry in interface IJarFile
      Overrides:
      getEntry in class JarFile
      Parameters:
      name - name of the Zip entry, 'winStyle' parameter applies, i.e. '\' is translated to '/' and the name check is done case insensitive.
      Returns:
      the ZipEntry found, or null for not found.
    • size

      public int size()
      Returns the number of entries in the Jar file.
      Overrides:
      size in class ZipFile
      Returns:
      the number of entries in the Jar file.
    • getManifest

      public Manifest getManifest()
      Returns the Jar file manifest, or null if none.
      Specified by:
      getManifest in interface IJarFile
      Overrides:
      getManifest in class JarFile
      Returns:
      the Jar file manifest, or null if none.
    • getName

      public String getName()
      Gets the name of the Jar file. If no name has been assigned using setName, the "real" temporary Jar file name from the temporary directory is returned.
      Specified by:
      getName in interface IJarFile
      Overrides:
      getName in class ZipFile
      Returns:
      the path name of the Zip/Jar file.
    • setName

      public void setName(String name)
      Sets the name of the Jar file. The getName method will return that name (if non-null), or the "real" temporary Jar file name from the temporary directory if the name parameter is null.
      Parameters:
      name - the name of the Jar file or null to reset it to the "real" temporary Jar file.
    • getEntryData

      public byte[] getEntryData(String name)
      Gets the byte [] data for an entry if present.
      Specified by:
      getEntryData in interface IJarFile
      Parameters:
      name - the name of the entry.
      Returns:
      the byte array or null if not found.
    • getEntryData

      public byte[] getEntryData(ZipEntry ze)
      Gets the byte [] data for an entry if present.
      Specified by:
      getEntryData in interface IJarFile
      Parameters:
      ze - the name of the entry.
      Returns:
      the byte array or null if not found.
    • close

      public void close() throws IOException
      Closes the ZIP file.

      Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface IJarFile
      Overrides:
      close in class ZipFile
      Throws:
      IOException - if an I/O error has occurred