public class Jar extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
Jar.Filter |
| Constructor and Description |
|---|
Jar()
Creates a new, empty, JAR
|
Jar(File jar)
Reads in the JAR from the given
File. |
Jar(InputStream jar)
Reads in the JAR from the given
InputStream. |
Jar(Jar jar)
Creates a copy
|
Jar(Path jar)
Reads in the JAR from the given
Path. |
Jar(String jar)
Reads in the JAR from the given path.
|
| Modifier and Type | Method and Description |
|---|---|
Jar |
addClass(Class<?> clazz)
Adds a class entry to this JAR.
|
Jar |
addEntries(Path path,
Path dirOrZip)
Adds a directory (with all its subdirectories) or the contents of a zip/JAR to this JAR.
|
Jar |
addEntries(Path path,
Path dirOrZip,
Jar.Filter filter)
Adds a directory (with all its subdirectories) or the contents of a zip/JAR to this JAR.
|
Jar |
addEntries(Path path,
ZipInputStream zip)
Adds the contents of the zip/JAR contained in the given byte array to this JAR.
|
Jar |
addEntries(Path path,
ZipInputStream zip,
Jar.Filter filter)
Adds the contents of the zip/JAR contained in the given byte array to this JAR.
|
Jar |
addEntries(String path,
Path dirOrZip)
Adds a directory (with all its subdirectories) or the contents of a zip/JAR to this JAR.
|
Jar |
addEntries(String path,
Path dirOrZip,
Jar.Filter filter)
Adds a directory (with all its subdirectories) or the contents of a zip/JAR to this JAR.
|
Jar |
addEntry(Path path,
InputStream is)
Adds an entry to this JAR.
|
Jar |
addEntry(Path path,
Path file)
Adds an entry to this JAR.
|
Jar |
addEntry(Path path,
String file)
Adds an entry to this JAR.
|
Jar |
addEntry(String path,
byte[] content)
Adds an entry to this JAR.
|
Jar |
addEntry(String path,
File file)
Adds an entry to this JAR.
|
Jar |
addEntry(String path,
InputStream is)
Adds an entry to this JAR.
|
Jar |
addEntry(String path,
String file)
Adds an entry to this JAR.
|
Jar |
addPackageOf(Class<?> clazz)
Adds the contents of a Java package to this JAR.
|
Jar |
addPackageOf(Class<?> clazz,
Jar.Filter filter)
Adds the contents of a Java package to this JAR.
|
Jar |
close() |
String |
getAttribute(String name)
Returns an attribute's value from this JAR's manifest's main section.
|
String |
getAttribute(String section,
String name)
Returns an attribute's value from a non-main section of this JAR's manifest.
|
List<String> |
getListAttribute(String name)
Returns an attribute's list value from this JAR's manifest's main section.
|
List<String> |
getListAttribute(String section,
String name)
Returns an attribute's list value from a non-main section of this JAR's manifest.
|
Manifest |
getManifest()
Returns the manifest of this JAR.
|
Map<String,String> |
getMapAttribute(String name,
String defaultValue)
Returns an attribute's map value from this JAR's manifest's main section.
|
Map<String,String> |
getMapAttribute(String section,
String name,
String defaultValue)
Returns an attribute's map value from a non-main section of this JAR's manifest.
|
static Jar.Filter |
matches(String regex) |
static Jar.Filter |
notMatches(String regex) |
Jar |
setAttribute(String name,
String value)
Sets an attribute in the main section of the manifest.
|
Jar |
setAttribute(String section,
String name,
String value)
Sets an attribute in a non-main section of the manifest.
|
Jar |
setJarPrefix(Path file)
Sets a file whose contents will be prepended to the JAR file's data.
|
Jar |
setJarPrefix(String value)
Sets a string that will be prepended to the JAR file's data.
|
Jar |
setListAttribute(String name,
Collection<?> values)
Sets an attribute in the main section of the manifest to a list.
|
Jar |
setListAttribute(String section,
String name,
Collection<?> values)
Sets an attribute in a non-main section of the manifest to a list.
|
Jar |
setMapAttribute(String name,
Map<String,?> values)
Sets an attribute in the main section of the manifest to a map.
|
Jar |
setMapAttribute(String section,
String name,
Map<String,?> values)
Sets an attribute in a non-main section of the manifest to a map.
|
Jar |
setOutput(File out)
|
Jar |
setOutput(Path out)
|
Jar |
setOutputStream(OutputStream os)
Sets an
OutputStream to which the JAR will be written. |
Jar |
setPacker(Pack200.Packer packer)
Sets a
Pack200 packer to use when writing the JAR. |
Jar |
setReallyExecutable(boolean value)
If set to true true, a header will be added to the JAR file when written, that will make the JAR an executable file in POSIX environments.
|
byte[] |
toByteArray()
Returns this JAR file as an array of bytes.
|
static InputStream |
toInputStream(String str,
Charset charset)
Turns a
String into an InputStream containing the string's encoded characters. |
File |
write(File file)
Writes this JAR to a file.
|
Path |
write(Path path)
Writes this JAR to a file.
|
void |
write(String file)
Writes this JAR to a file.
|
<T extends OutputStream> |
write(T os)
Writes this JAR to an output stream, and closes the stream.
|
public Jar()
public Jar(InputStream jar) throws IOException
InputStream.
Modifications will not be made to the original JAR file, but to a new copy, which is then written with write().IOExceptionpublic Jar(Path jar) throws IOException
Path.
Modifications will not be made to the original JAR file, but to a new copy, which is then written with write().IOExceptionpublic Jar(File jar) throws IOException
File.
Modifications will not be made to the original JAR file, but to a new copy, which is then written with write().IOExceptionpublic Jar(String jar) throws IOException
write().IOExceptionpublic Jar(Jar jar)
public Manifest getManifest()
addEntry().public final Jar setAttribute(String name, String value)
name - the attribute's namevalue - the attribute's valuethisIllegalStateException - if entries have been added or the JAR has been written prior to calling this methods.public final Jar setAttribute(String section, String name, String value)
section - the section's namename - the attribute's namevalue - the attribute's valuethisIllegalStateException - if entries have been added or the JAR has been written prior to calling this methods.public Jar setListAttribute(String name, Collection<?> values)
name - the attribute's namevalues - the attribute's valuethisIllegalStateException - if entries have been added or the JAR has been written prior to calling this methods.public Jar setListAttribute(String section, String name, Collection<?> values)
section - the section's namename - the attribute's namevalues - the attribute's valuethisIllegalStateException - if entries have been added or the JAR has been written prior to calling this methods.public Jar setMapAttribute(String name, Map<String,?> values)
name - the attribute's namevalues - the attribute's valuethisIllegalStateException - if entries have been added or the JAR has been written prior to calling this methods.public Jar setMapAttribute(String section, String name, Map<String,?> values)
section - the section's namename - the attribute's namevalues - the attribute's valuethisIllegalStateException - if entries have been added or the JAR has been written prior to calling this methods.public String getAttribute(String name)
name - the attribute's namepublic String getAttribute(String section, String name)
section - the manifest's sectionname - the attribute's namepublic List<String> getListAttribute(String name)
name - the attribute's namepublic List<String> getListAttribute(String section, String name)
section - the manifest's sectionname - the attribute's namepublic Map<String,String> getMapAttribute(String name, String defaultValue)
name - the attribute's namepublic Map<String,String> getMapAttribute(String section, String name, String defaultValue)
section - the manifest's sectionname - the attribute's namepublic Jar addEntry(String path, InputStream is) throws IOException
path - the entry's path within the JARis - the entry's contentthisIOExceptionpublic Jar addEntry(String path, byte[] content) throws IOException
path - the entry's path within the JARcontent - the entry's contentthisIOExceptionpublic Jar addEntry(Path path, InputStream is) throws IOException
path - the entry's path within the JARis - the entry's contentthisIOExceptionpublic Jar addEntry(Path path, Path file) throws IOException
path - the entry's path within the JARfile - the file to add as an entrythisIOExceptionpublic Jar addEntry(Path path, String file) throws IOException
path - the entry's path within the JARfile - the path of the file to add as an entrythisIOExceptionpublic Jar addEntry(String path, File file) throws IOException
path - the entry's path within the JARfile - the file to add as an entrythisIOExceptionpublic Jar addEntry(String path, String file) throws IOException
path - the entry's path within the JARfile - the path of the file to add as an entrythisIOExceptionpublic Jar addClass(Class<?> clazz) throws IOException
clazz - the class to add to the JAR.thisIOExceptionpublic Jar addEntries(Path path, Path dirOrZip, Jar.Filter filter) throws IOException
path - the path within the JAR where the root of the directory will be placed, or null for the JAR's rootdirOrZip - the directory to add as an entry or a zip/JAR file whose contents will be extracted and added as entriesfilter - a filter to select particular classesthisIOExceptionpublic Jar addEntries(Path path, Path dirOrZip) throws IOException
path - the path within the JAR where the root of the directory will be placed, or null for the JAR's rootdirOrZip - the directory to add as an entry or a zip/JAR file whose contents will be extracted and added as entriesthisIOExceptionpublic Jar addEntries(String path, Path dirOrZip, Jar.Filter filter) throws IOException
path - the path within the JAR where the root of the directory/zip will be placed, or null for the JAR's rootdirOrZip - the directory to add as an entry or a zip/JAR file whose contents will be extracted and added as entriesfilter - a filter to select particular classesthisIOExceptionpublic Jar addEntries(String path, Path dirOrZip) throws IOException
path - the path within the JAR where the root of the directory/zip will be placed, or null for the JAR's rootdirOrZip - the directory to add as an entry or a zip/JAR file whose contents will be extracted and added as entriesthisIOExceptionpublic Jar addEntries(Path path, ZipInputStream zip) throws IOException
path - the path within the JAR where the root of the zip will be placed, or null for the JAR's rootzip - the contents of the zip/JAR filethisIOExceptionpublic Jar addEntries(Path path, ZipInputStream zip, Jar.Filter filter) throws IOException
path - the path within the JAR where the root of the zip will be placed, or null for the JAR's rootzip - the contents of the zip/JAR filefilter - a filter to select particular classesthisIOExceptionpublic Jar addPackageOf(Class<?> clazz) throws IOException
clazz - A class whose package we wish to add to the JAR.thisIOExceptionpublic Jar addPackageOf(Class<?> clazz, Jar.Filter filter) throws IOException
clazz - a class whose package we wish to add to the JAR.filter - a filter to select particular classesthisIOExceptionpublic Jar setPacker(Pack200.Packer packer)
Pack200 packer to use when writing the JAR.packer - thispublic Jar setReallyExecutable(boolean value)
value - thispublic Jar setJarPrefix(String value)
value - the prefix, or null for none.thispublic Jar setJarPrefix(Path file)
file - the prefix file, or null for none.thispublic Jar setOutputStream(OutputStream os)
OutputStream to which the JAR will be written.
If used, this method must be called before any entries have been added or the JAR written. Calling this method prevents this
object from using an internal buffer to store the JAR, and therefore, none of the other write methods can be called.os - the target OutputStream of this JAR.thispublic Jar setOutput(Path out) throws IOException
setOutputStream(Files.newOutputStream(out)).
If used, this method must be called before any entries have been added or the JAR written. Calling this method prevents this
object from using an internal buffer to store the JAR, and therefore, none of the other write methods can be called.out - the target file to which this JAR will be written.thisIOExceptionpublic Jar setOutput(File out) throws IOException
setOutputStream(new FileOutputStream(out)).
If used, this method must be called before any entries have been added or the JAR written. Calling this method prevents this
object from using an internal buffer to store the JAR, and therefore, none of the other write methods can be called.out - the target file to which this JAR will be written.thisIOExceptionpublic Jar close() throws IOException
IOExceptionpublic <T extends OutputStream> T write(T os) throws IOException
IOExceptionpublic File write(File file) throws IOException
IOExceptionpublic Path write(Path path) throws IOException
IOExceptionpublic void write(String file) throws IOException
IOExceptionpublic byte[] toByteArray()
public static InputStream toInputStream(String str, Charset charset)
String into an InputStream containing the string's encoded characters.str - the stringcharset - the Charset to use when encoding the string.InputStream containing the string's encoded characters.public static Jar.Filter matches(String regex)
public static Jar.Filter notMatches(String regex)