Java by API/java.util.jar/Manifest — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:43, 31 мая 2010

Manifest: getEntries()

 
import java.util.Iterator;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class Main {
  public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile("filename.jar");
    Manifest manifest = jarfile.getManifest();
    Map map = manifest.getEntries();
    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
      String entryName = (String) it.next();
      Attributes attrs = (Attributes) map.get(entryName);
      for (Iterator it2 = attrs.keySet().iterator(); it2.hasNext();) {
        Attributes.Name attrName = (Attributes.Name) it2.next();
        String attrValue = attrs.getValue(attrName);
      }
    }
  }
}





Manifest: getMainAttributes()

 
import java.util.Iterator;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class Main {
  public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile("filename.jar");
    Manifest manifest = jarfile.getManifest();
    Attributes attrs = (Attributes) manifest.getMainAttributes();
    for (Iterator it = attrs.keySet().iterator(); it.hasNext();) {
      Attributes.Name attrName = (Attributes.Name) it.next();
      String attrValue = attrs.getValue(attrName);
    }
  }
}





new Manifest(InputStream is)

 
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.jar.Manifest;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a manifest from a file
    InputStream fis = new FileInputStream("manifestfile");
    Manifest manifest = new Manifest(fis);
    // Construct a string version of a manifest
    StringBuffer sbuf = new StringBuffer();
    sbuf.append("Manifest-Version: 1.0\n");
    sbuf.append("\n");
    sbuf.append("Name: javax/swing/JScrollPane.class\n");
    sbuf.append("Java-Bean: True\n");
    // Convert the string to a input stream
    InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));
    // Create the manifest
    manifest = new Manifest(is);
  }
}