java.util.jar.Manifest类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(361)

本文整理了Java中java.util.jar.Manifest类的一些代码示例,展示了Manifest类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Manifest类的具体详情如下:
包路径:java.util.jar.Manifest
类名称:Manifest

Manifest介绍

[英]The Manifest class is used to obtain attribute information for a JarFile and its entries.
[中]Manifest类用于获取JarFile及其条目的属性信息。

代码示例

代码示例来源:origin: skylot/jadx

public static String getVersion() {
    try {
      ClassLoader classLoader = Jadx.class.getClassLoader();
      if (classLoader != null) {
        Enumeration<URL> resources = classLoader.getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
          Manifest manifest = new Manifest(resources.nextElement().openStream());
          String ver = manifest.getMainAttributes().getValue("jadx-version");
          if (ver != null) {
            return ver;
          }
        }
      }
    } catch (Exception e) {
      LOG.error("Can't get manifest file", e);
    }
    return "dev";
  }
}

代码示例来源:origin: Sable/soot

private void addManifest(ZipOutputStream destination, Collection<File> dexFiles) throws IOException {
 final Manifest manifest = new Manifest();
 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
 manifest.getMainAttributes().put(new Attributes.Name("Created-By"), "Soot Dex Printer");
 if (dexFiles != null && !dexFiles.isEmpty()) {
  manifest.getMainAttributes().put(new Attributes.Name("Dex-Location"),
    dexFiles.stream().map(File::getName).collect(Collectors.joining(" ")));
 }
 final ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
 destination.putNextEntry(manifestEntry);
 manifest.write(new BufferedOutputStream(destination));
 destination.closeEntry();
}

代码示例来源:origin: pxb1988/dex2jar

Manifest input = jar.getManifest();
Manifest output = new Manifest();
Attributes main = output.getMainAttributes();
if (input != null) {
  main.putAll(input.getMainAttributes());
main.putValue("Manifest-Version", "1.0");
main.putValue("Created-By", "1.6.0_21 (d2j-" + AbstractJarSign.class.getPackage().getImplementationVersion() + ")");
for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
  JarEntry entry = e.nextElement();
  byName.put(entry.getName(), entry);
  String name = entry.getName();
  if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !stripPattern.matcher(name).matches()) {
    InputStream data = jar.getInputStream(entry);
    while ((num = data.read(buffer)) > 0) {
      md.update(buffer, 0, num);
      attr = input.getAttributes(name);
    attr = attr != null ? new Attributes(attr) : new Attributes();
    attr.putValue(digName, encodeBase64(md.digest()));
    output.getEntries().put(name, attr);

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public File toJar(File file) throws IOException {
  Manifest manifest = new Manifest();
  manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, MANIFEST_VERSION);
  return toJar(file, manifest);
}

代码示例来源:origin: robovm/robovm

private boolean isSealed(Manifest manifest, String dirName) {
  Attributes attributes = manifest.getAttributes(dirName);
  if (attributes != null) {
    String value = attributes.getValue(Attributes.Name.SEALED);
    if (value != null) {
      return value.equalsIgnoreCase("true");
    }
  }
  Attributes mainAttributes = manifest.getMainAttributes();
  String value = mainAttributes.getValue(Attributes.Name.SEALED);
  return (value != null && value.equalsIgnoreCase("true"));
}

代码示例来源:origin: Netflix/eureka

private static Manifest loadManifest(String jarUrl) throws Exception {
  InputStream is = new URL(jarUrl + "!/META-INF/MANIFEST.MF").openStream();
  try {
    return new Manifest(is);
  } finally {
    is.close();
  }
}

代码示例来源:origin: stackoverflow.com

JarFile jar = new JarFile(new File("path/to/your/jar-file"));
InputStream is = jar.getInputStream(jar.getEntry("META-INF/MANIFEST.MF"));
Manifest man = new Manifest(is);
is.close();
for(Map.Entry<String, Attributes> entry: man.getEntries().entrySet()) {
  for(Object attrkey: entry.getValue().keySet()) {
    if (attrkey instanceof Attributes.Name && 
      ((Attributes.Name)attrkey).toString().indexOf("-Digest") != -1)
      signed.add(entry.getKey());
for(Enumeration<JarEntry> entry = jar.entries(); entry.hasMoreElements(); ) {
  JarEntry je = entry.nextElement();
  if (!je.isDirectory())
    entries.add(je.getName());

代码示例来源:origin: stackoverflow.com

Enumeration<URL> resources = getClass().getClassLoader()
 .getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
  try {
   Manifest manifest = new Manifest(resources.nextElement().openStream());
   // check that this is your manifest and do what you need or get the next one
   ...
  } catch (IOException E) {
   // handle
  }
}

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public Manifest getManifest() throws IOException {
  File file = new File(folder, JarFile.MANIFEST_NAME);
  if (file.exists()) {
    InputStream inputStream = new FileInputStream(file);
    try {
      return new Manifest(inputStream);
    } finally {
      inputStream.close();
    }
  } else {
    return NO_MANIFEST;
  }
}

代码示例来源:origin: redisson/redisson

try {
  try {
    Manifest manifest = new Manifest(inputStream);
    Map<Attributes.Name, String> values = new HashMap<Attributes.Name, String>();
    Attributes mainAttributes = manifest.getMainAttributes();
    if (mainAttributes != null) {
      for (Attributes.Name attributeName : ATTRIBUTE_NAMES) {
        values.put(attributeName, mainAttributes.getValue(attributeName));
    Attributes attributes = manifest.getAttributes(packageName.replace('.', '/').concat("/"));
    if (attributes != null) {
      for (Attributes.Name attributeName : ATTRIBUTE_NAMES) {
        String value = attributes.getValue(attributeName);
        if (value != null) {
          values.put(attributeName, value);
            : NOT_SEALED);
  } finally {
    inputStream.close();

代码示例来源:origin: jenkinsci/jenkins

URL sealBase = null;
Attributes sectionAttributes = manifest.getAttributes(sectionName);
if (sectionAttributes != null) {
  specificationTitle = sectionAttributes.getValue(Name.SPECIFICATION_TITLE);
  specificationVendor = sectionAttributes.getValue(Name.SPECIFICATION_VENDOR);
  specificationVersion = sectionAttributes.getValue(Name.SPECIFICATION_VERSION);
  implementationTitle = sectionAttributes.getValue(Name.IMPLEMENTATION_TITLE);
  implementationVendor = sectionAttributes.getValue(Name.IMPLEMENTATION_VENDOR);
  sealedString = sectionAttributes.getValue(Name.SEALED);
Attributes mainAttributes = manifest.getMainAttributes();
if (mainAttributes != null) {
  if (specificationTitle == null) {
    sealBase = new URL(FileUtils.getFileUtils().toURI(container.getAbsolutePath()));
  } catch (MalformedURLException e) {

代码示例来源:origin: pxb1988/dex2jar

int num;
Map<String, Attributes> entries = manifest.getEntries();
List<String> names = new ArrayList<>(entries.keySet());
Collections.sort(names);
for (String name : names) {
  JarEntry inEntry = in.getJarEntry(name);
  JarEntry outEntry = null;
  if (inEntry.getMethod() == JarEntry.STORED) {
  out.putNextEntry(outEntry);
  InputStream data = in.getInputStream(inEntry);
  while ((num = data.read(buffer)) > 0) {
    out.write(buffer, 0, num);

代码示例来源:origin: jenkinsci/jenkins

protected String identifyPluginShortName(File t) {
  try {
    JarFile j = new JarFile(t);
    try {
      String name = j.getManifest().getMainAttributes().getValue("Short-Name");
      if (name!=null) return name;
    } finally {
      j.close();
    }
  } catch (IOException e) {
    LOGGER.log(WARNING, "Failed to identify the short name from "+t,e);
  }
  return FilenameUtils.getBaseName(t.getName());    // fall back to the base name of what's uploaded
}

代码示例来源:origin: biz.aQute/bndlib

public Manifest getManifest() throws Exception {
  check();
  if (manifest == null) {
    Resource manifestResource = getResource("META-INF/MANIFEST.MF");
    if (manifestResource != null) {
      InputStream in = manifestResource.openInputStream();
      manifest = new Manifest(in);
      in.close();
    }
  }
  return manifest;
}

代码示例来源:origin: google/guava

manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH.toString());
if (classpathAttribute != null) {
 for (String path : CLASS_PATH_ATTRIBUTE_SEPARATOR.split(classpathAttribute)) {
   continue;
  if (url.getProtocol().equals("file")) {
   builder.add(toFile(url));

代码示例来源:origin: stackoverflow.com

URLClassLoader cl = (URLClassLoader) getClass().getClassLoader();
try {
 URL url = cl.findResource("META-INF/MANIFEST.MF");
 Manifest manifest = new Manifest(url.openStream());
 // do stuff with it
 ...
} catch (IOException E) {
 // handle
}

代码示例来源:origin: jenkinsci/jenkins

/*package*/ static @CheckForNull Manifest parsePluginManifest(URL bundledJpi) {
  try {
    URLClassLoader cl = new URLClassLoader(new URL[]{bundledJpi});
    InputStream in=null;
    try {
      URL res = cl.findResource(PluginWrapper.MANIFEST_FILENAME);
      if (res!=null) {
        in = getBundledJpiManifestStream(res);
        Manifest manifest = new Manifest(in);
        return manifest;
      }
    } finally {
      Util.closeAndLogFailures(in, LOGGER, PluginWrapper.MANIFEST_FILENAME, bundledJpi.toString());
      if (cl instanceof Closeable)
        ((Closeable)cl).close();
    }
  } catch (IOException e) {
    LOGGER.log(WARNING, "Failed to parse manifest of "+bundledJpi, e);
  }
  return null;
}

代码示例来源:origin: jenkinsci/jenkins

private String getVersionOf(Manifest manifest) {
  String v = manifest.getMainAttributes().getValue("Plugin-Version");
  if(v!=null)      return v;
  // plugins generated before maven-hpi-plugin 1.3 should still have this attribute
  v = manifest.getMainAttributes().getValue("Implementation-Version");
  if(v!=null)      return v;
  return "???";
}

代码示例来源:origin: org.apache.felix/maven-bundle-plugin

private boolean isOsgi( Jar jar ) throws Exception
{
  if ( jar.getManifest() != null )
  {
    return jar.getManifest().getMainAttributes().getValue( Analyzer.BUNDLE_NAME ) != null;
  }
  return false;
}

代码示例来源:origin: gocd/gocd

private static String defaultMainClassName(JarFile jarFile) throws IOException {
    return jarFile.getManifest().getMainAttributes().getValue("GoCD-Main-Class");
  }
}

相关文章