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檔案中的資源參照來構造URL物件。JAR檔案URL
語法為 -
jar:<url>!/{entry}
以下URL使用HTTP協定參照www.tw511.com
上的test.jar
檔案中的images/logo.bmp
JAR條目如下:
jar:https://www.tw511.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");