JAR API
JAR API包括使用 manifest
文件的类。Manifest
类的一个对象表示一个manifest
文件。 在代码中创建一个Manifest
对象,如下所示:
Manifest manifest = new Manifest()
可以从manifest
文件中读取条目并向其写入条目。要将一个条目添加到主部分,使用Manifest
类中的getMainAttributes()
方法获取Attributes
类的实例,并使用其put()
方法继续向其添加名称/值对。
以下代码将一些属性添加到manifest
对象的主部分。已知的属性名称在Attributes.Name
类中定义为常量。
例如,常量Attributes.Name.MANIFEST_VERSION
表示manifest
版本属性名称。
Manifest manifest = new Manifest()
Attributes mainAttribs = manifest.getMainAttributes() mainAttribs.put(Attributes.Name.MANIFEST_VERSION, "1.0") mainAttribs.put(Attributes.Name.MAIN_CLASS, "com.yiibai.Main") mainAttribs.put(Attributes.Name.SEALED, "true")
将单个条目添加到manifest
文件比添加到主条目稍微复杂一点。以下代码显示如何向Manifest
对象添加单个条目:
Map<String,Attributes> attribsMap = manifest.getEntries()
Attributes attribs = new Attributes()
Attributes.Name name = new Attributes.Name("Sealed")
attribs.put(name, "false")
attribsMap.put("com/yiibai/archives/", attribs)
要将manifest
文件添加到JAR文件,请在JarOutputStream
类的一个构造函数中指定它。例如,以下代码创建一个jar
输出流,以使用Manifest
对象创建一个test.jar
文件:
Manifest manifest = new Manifest()
JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(
new FileOutputStream("test.jar")), manifest)
以下代码创建包含Manifest
文件的JAR文件。
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.util.Map
import java.util.jar.Attributes
import java.util.jar.JarEntry
import java.util.jar.JarOutputStream
import java.util.jar.Manifest
import java.util.zip.Deflater
public class Main {
public static void main(String[] args) throws Exception {
Manifest manifest = getManifest()
String jarFileName = "jartest.jar"
String[] entries = new String[2]
entries[0] = "images/logo.bmp"
entries[1] = "com/yiibai/Test.class"
createJAR(jarFileName, entries, manifest)
}
public static void createJAR(String jarFileName, String[] jarEntries,
Manifest manifest) {
try (JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(
new FileOutputStream(jarFileName)), manifest)) {
jos.setLevel(Deflater.BEST_COMPRESSION)
for (int i = 0 i < jarEntries.length i++) {
File entryFile = new File(jarEntries[i])
if (!entryFile.exists()) {
return
}
JarEntry je = new JarEntry(jarEntries[i])
jos.putNextEntry(je)
addEntryContent(jos, jarEntries[i])
jos.closeEntry()
}
} catch (IOException e) {
e.printStackTrace()
}
}
public static void addEntryContent(JarOutputStream jos, String entryFileName)
throws IOException, FileNotFoundException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
entryFileName))
byte[] buffer = new byte[1024]
int count = -1
while ((count = bis.read(buffer)) != -1) {
jos.write(buffer, 0, count)
}
bis.close()
}
public static Manifest getManifest() {
Manifest manifest = new Manifest()
Attributes mainAttribs = manifest.getMainAttributes()
mainAttribs.put(Attributes.Name.MANIFEST_VERSION, "1.0")
mainAttribs.put(Attributes.Name.MAIN_CLASS, "com.yiibai.Test")
mainAttribs.put(Attributes.Name.SEALED, "true")
Map<String, Attributes> attribsMap = manifest.getEntries()
Attributes a1 = getAttribute("Sealed", "false")
attribsMap.put("com/yiibai/", a1)
Attributes a2 = getAttribute("Content-Type", "image/bmp")
attribsMap.put("images/logo.bmp", a2)
return manifest
}
public static Attributes getAttribute(String name, String value) {
Attributes a = new Attributes()
Attributes.Name attribName = new Attributes.Name(name)
a.put(attribName, value)
return a
}
}
要从JAR文件的清单文件读取条目,请使用JarInputStream
的getManifest()
类获取Manifest
类的对象,如下所示:
JarInputStream jis = new JarInputStream(new FileInputStream("jartest.jar"))
Manifest manifest = jis.getManifest()
if (manifest != null) {
Attributes mainAttributes = manifest.getMainAttributes()
String mainClass = mainAttributes.getValue("Main-Class")
Map<String, Attributes> entries = manifest.getEntries()
}
从JAR文件访问资源
可以通过使用JAR文件中的资源引用来构造URL对象。JAR文件URL
语法为 -
jar:<url>!/{entry}
以下URL使用HTTP协议引用www.yiibai.com
上的test.jar
文件中的images/logo.bmp
JAR条目如下:
jar:http://www.yiibai.com/test.jar!/images/logo.bmp
以下URL使用文件协议引用c:\jarfiles\
目录中的本地文件系统上的test.jar
文件中的images/logo.bmp
JAR条目:
jar:file:/c:/jarfiles/test.jar!/images/logo.bmp
要从类路径中的JAR文件读取images/logo.bmp
文件,可以使用类对象获取输入流对象,如下所示:
// Assuming that the Test class is in the CLASSPATH
Class cls = Test.class
InputStream in = cls.getResourceAsStream("/images/logo.bmp")
还可以在JAR文件中获取一个条目的URL对象,该路径在类路径中如下所示:
URL url = cls.getResource("/images/logo.bmp")