Java Jar API

2018-02-05 17:39 更新

Java IO教程 - Java Jar API

JAR API

JAR API包括使用清單文件的類。 Manifest類的一個(gè)對(duì)象表示一個(gè)清單文件。在代碼中創(chuàng)建一個(gè)Manifest對(duì)象,如下所示:

Manifest manifest = new Manifest();

我們可以從清單文件中讀取條目并向其寫入條目。

要將一個(gè)條目添加到主要部分,使用Manifest類中的getMainAttributes()方法獲取Attributes類的實(shí)例,并使用其put()方法繼續(xù)向其添加名稱/值對(duì)。

以下代碼將一些屬性添加到清單對(duì)象的主要部分。已知的屬性名稱在Attributes.Name類中定義為常量。

例如,常量Attributes.Name.MANIFEST_VERSION表示清單版本屬性名稱。

Manifest manifest = new Manifest();
Attributes  mainAttribs = manifest.getMainAttributes(); mainAttribs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); mainAttribs.put(Attributes.Name.MAIN_CLASS, "com.w3cschool.Main"); mainAttribs.put(Attributes.Name.SEALED, "true");

將單個(gè)條目添加到清單文件比添加主條目稍微復(fù)雜一些。

以下代碼顯示如何向Manifest對(duì)象添加單個(gè)條目:

Map<String,Attributes> attribsMap = manifest.getEntries();
Attributes attribs  = new Attributes();
Attributes.Name name = new Attributes.Name("Sealed");
attribs.put(name, "false");
attribsMap.put("com/w3cschool/archives/", attribs);

要將清單文件添加到JAR文件,請(qǐng)?jiān)贘arOutputStream類的一個(gè)構(gòu)造函數(shù)中指定它。

例如,以下代碼創(chuàng)建一個(gè)jar輸出流,以使用Manifest對(duì)象創(chuàng)建一個(gè)test.jar文件:

Manifest manifest = new Manifest();
JarOutputStream jos  = new JarOutputStream(new BufferedOutputStream(
new FileOutputStream("test.jar")), manifest);

以下代碼創(chuàng)建包含清單文件的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/w3cschool/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.w3cschool.Test");
    mainAttribs.put(Attributes.Name.SEALED, "true");

    Map<String, Attributes> attribsMap = manifest.getEntries();

    Attributes a1 = getAttribute("Sealed", "false");
    attribsMap.put("com/w3cschool/", 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文件的清單文件讀取條目,請(qǐng)使用JarInputStream的getManifest()類獲取Manifest類的對(duì)象,如下所示:

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文件中的資源引用來構(gòu)造URL對(duì)象。

JAR文件URL語法為

jar:<url>!/{entry}

以下URL使用HTTP協(xié)議引用hgci.cn上的test.jar文件中的images/logo.bmp JAR條目:

jar://hgci.cn/test.jar!/images/logo.bmp

以下URL使用文件協(xié)議引用c:\jarfiles\目錄中的本地文件系統(tǒng)上的test.jar文件中的images / logo.bmp JAR條目:

jar:file:/c:/jarfiles/test.jar!/images/logo.bmp

要從類路徑中的JAR文件讀取images/logo.bmp文件,可以使用類對(duì)象獲取輸入流對(duì)象,如下所示:

// Assuming that the   Test   class is in the   CLASSPATH 
Class cls = Test.class;
InputStream in = cls.getResourceAsStream("/images/logo.bmp")

您還可以在JAR文件中獲取一個(gè)條目的URL對(duì)象,該路徑在類路徑中如下所示:

URL  url = cls.getResource("/images/logo.bmp");


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)