Java Jar API

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文件的清單文件讀取條目,請使用JarInputStreamgetManifest()類獲取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");