Class JarModifier
- java.lang.Object
-
- com.ensoftcorp.open.java.commons.bytecode.JarModifier
-
public class JarModifier extends java.lang.Object
A wrapper around the Java zip utilities to add, overwrite, or remove files from archives.- Author:
- Ben Holland
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MANIFEST_PATH
The standard manifest pathstatic java.lang.String
META_INF
The directory that stores the manifest and jar signaturesstatic java.lang.String
SEPERATOR
The directory separator character for archive files as a string
-
Constructor Summary
Constructors Constructor Description JarModifier(java.io.File jarFile)
Creates a new JarModifier with the given archive to be modified
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(java.lang.String entry, byte[] bytes, boolean overwrite)
Adds (or optionally overwrites) an archive entryvoid
add(java.util.jar.JarEntry entry, byte[] bytes, boolean overwrite)
Adds (or optionally overwrites) an archive entry with the specified entry properties.byte[]
extractEntry(java.lang.String entry)
static java.util.jar.Manifest
generateEmptyManifest()
Generates a Jar Manifest based on the given parametersjava.util.HashSet<java.lang.String>
getJarEntrySet()
java.io.File
getJarFile()
java.util.jar.Manifest
getManifest()
Returns the parsed manifest or null if there is no manifestvoid
remove(java.lang.String entry)
Removes the specified entry if one exits (example: a/b/c/test.txt)void
remove(java.util.jar.JarEntry entry)
Removes the specified entry if one exits (example: a/b/c/test.txt)void
removeFilesWithName(java.lang.String filename)
Removes any entries with a matching file name (example: test.txt)void
removeSubdirectory(java.lang.String directory)
Removes any entries with the matching file name prefixvoid
save(java.io.File outputArchiveFile)
Writes the modified output archive to a filejava.lang.String
toString()
Prints the contents of the archive file if it were written to diskstatic void
unjar(java.io.File inputJar, java.io.File outputPath)
Extracts a Jar filevoid
unsign()
From: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jarsigner.html When jarsigner is used to sign a JAR file, the output signed JAR file is exactly the same as the input JAR file, except that it has two additional files placed in the META-INF directory: a signature file, with a .SF extension, and a signature block file, with a .DSA, .RSA, or .EC extension.
-
-
-
Field Detail
-
SEPERATOR
public static final java.lang.String SEPERATOR
The directory separator character for archive files as a string- See Also:
- Constant Field Values
-
META_INF
public static final java.lang.String META_INF
The directory that stores the manifest and jar signatures- See Also:
- Constant Field Values
-
MANIFEST_PATH
public static final java.lang.String MANIFEST_PATH
The standard manifest path- See Also:
- Constant Field Values
-
-
Method Detail
-
unjar
public static void unjar(java.io.File inputJar, java.io.File outputPath) throws java.io.IOException
Extracts a Jar file- Parameters:
inputJar
-outputPath
-- Throws:
java.io.IOException
-
getJarFile
public java.io.File getJarFile()
-
extractEntry
public byte[] extractEntry(java.lang.String entry) throws java.io.IOException
- Throws:
java.io.IOException
-
getManifest
public java.util.jar.Manifest getManifest()
Returns the parsed manifest or null if there is no manifest- Returns:
-
getJarEntrySet
public java.util.HashSet<java.lang.String> getJarEntrySet()
-
unsign
public void unsign()
From: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jarsigner.html When jarsigner is used to sign a JAR file, the output signed JAR file is exactly the same as the input JAR file, except that it has two additional files placed in the META-INF directory: a signature file, with a .SF extension, and a signature block file, with a .DSA, .RSA, or .EC extension. The method deletes the jarsigner signature file and the signature block files.
-
generateEmptyManifest
public static java.util.jar.Manifest generateEmptyManifest()
Generates a Jar Manifest based on the given parameters- Returns:
-
add
public void add(java.lang.String entry, byte[] bytes, boolean overwrite) throws java.io.IOException
Adds (or optionally overwrites) an archive entry- Parameters:
entry
- The entry path (example a/b/c/test.txt)file
- The contents of the file to addoverwrite
- True if an existing entry should be overwritten- Throws:
java.io.IOException
- Thrown if overwrite is false and the archive already contains the specified entry
-
add
public void add(java.util.jar.JarEntry entry, byte[] bytes, boolean overwrite) throws java.io.IOException
Adds (or optionally overwrites) an archive entry with the specified entry properties.- Parameters:
entry
- JarEntry with the properties to add or overwritefile
- The contents of the file to addoverwrite
- True if an existing entry should be overwritten- Throws:
java.io.IOException
- Thrown if overwrite is false and the archive already contains the specified entry
-
remove
public void remove(java.lang.String entry)
Removes the specified entry if one exits (example: a/b/c/test.txt)- Parameters:
entry
-
-
remove
public void remove(java.util.jar.JarEntry entry)
Removes the specified entry if one exits (example: a/b/c/test.txt)- Parameters:
entry
-
-
removeSubdirectory
public void removeSubdirectory(java.lang.String directory)
Removes any entries with the matching file name prefix- Parameters:
directory
-
-
removeFilesWithName
public void removeFilesWithName(java.lang.String filename)
Removes any entries with a matching file name (example: test.txt)- Parameters:
filename
- The filename to match
-
save
public void save(java.io.File outputArchiveFile) throws java.io.IOException
Writes the modified output archive to a file- Parameters:
outputArchive
-- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
Prints the contents of the archive file if it were written to disk- Overrides:
toString
in classjava.lang.Object
-
-