下面列出了java.util.jar.JarFile#MANIFEST_NAME 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
throws IOException {
Preconditions.checkNotNull(relativePath, "relativePath");
Preconditions.checkNotNull(zos, "zos");
// by JAR spec, if there is a manifest, it must be the first entry in the
// ZIP.
File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
if (!manifestFile.exists()) {
zos.putNextEntry(manifestEntry);
new Manifest().write(new BufferedOutputStream(zos));
zos.closeEntry();
} else {
copyToZipStream(manifestFile, manifestEntry, zos);
}
zos.closeEntry();
zipDir(dir, relativePath, zos, true);
zos.close();
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
@Test
public void testManifest() throws Exception {
File file = new File(folder, JarFile.MANIFEST_NAME);
assertThat(file.getParentFile().mkdir(), is(true));
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
OutputStream outputStream = new FileOutputStream(file);
try {
manifest.write(outputStream);
} finally {
outputStream.close();
}
Plugin.Engine.Source.Origin origin = new Plugin.Engine.Source.ForFolder(folder).read();
try {
Manifest readManifest = origin.getManifest();
assertThat(readManifest, notNullValue(Manifest.class));
assertThat(readManifest.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION), is("1.0"));
} finally {
origin.close();
}
assertThat(file.delete(), is(true));
assertThat(file.getParentFile().delete(), is(true));
}
@Test
public void findMainClass_jar_normal() throws Exception {
URL url = PowerMockito.mock(URL.class);
String command = "a.jar";
String manifestUri = "jar:file:" + new File(command).getAbsolutePath() + "!/" + JarFile.MANIFEST_NAME;
PowerMockito.whenNew(URL.class).withParameterTypes(String.class)
.withArguments(manifestUri).thenReturn(url);
String content = String.format("Manifest-Version: 1.0\nMain-Class: %s\n", TestJvmUtils.class.getName());
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
PowerMockito.when(url.openStream()).thenReturn(inputStream);
System.setProperty(JvmUtils.SUN_JAVA_COMMAND, command + " arg");
Assert.assertEquals(TestJvmUtils.class, JvmUtils.findMainClass());
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
private static Attributes getDirAttributes(File dir) {
File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
if (manifestFile.isFile()) {
ByteSource source = Files.asByteSource(manifestFile);
InputStream inputStream = null;
try {
inputStream = source.openBufferedStream();
Manifest manifest = new Manifest(inputStream);
return manifest.getMainAttributes();
} catch (IOException ex) {
// be quiet checkstyle
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
// ignore
}
}
}
return null;
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
@Test
public void testManifest() throws Exception {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
Plugin.Engine.Target target = new Plugin.Engine.Target.ForFolder(folder);
target.write(manifest).close();
File file = new File(folder, JarFile.MANIFEST_NAME);
assertThat(file.isFile(), is(true));
InputStream inputStream = new FileInputStream(file);
try {
Manifest readManifest = new Manifest(inputStream);
assertThat(readManifest.getMainAttributes().get(Attributes.Name.MANIFEST_VERSION), is((Object) "1.0"));
} finally {
inputStream.close();
}
assertThat(file.delete(), is(true));
assertThat(file.getParentFile().delete(), is(true));
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
throws IOException {
Preconditions.checkNotNull(relativePath, "relativePath");
Preconditions.checkNotNull(zos, "zos");
// by JAR spec, if there is a manifest, it must be the first entry in the
// ZIP.
File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
if (!manifestFile.exists()) {
zos.putNextEntry(manifestEntry);
new Manifest().write(new BufferedOutputStream(zos));
zos.closeEntry();
} else {
copyToZipStream(manifestFile, manifestEntry, zos);
}
zos.closeEntry();
zipDir(dir, relativePath, zos, true);
zos.close();
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
/**
* Create the in-memory JAR containing all the classes in this module.
*/
public byte[] createJar(long blockTimeStamp) throws IOException {
// set jar file timestamp to block timestamp so the whole network is in agreement over this.
FileTime timestamp = FileTime.fromMillis(blockTimeStamp);
// manifest, we explicitly write it so that can can control its timestamps.
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, this.mainClass);
ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
manifestEntry.setLastModifiedTime(timestamp);
manifestEntry.setLastAccessTime(timestamp);
manifestEntry.setCreationTime(timestamp);
// Create a temporary memory location for this JAR.
ByteArrayOutputStream tempJarStream = new ByteArrayOutputStream(MAX_JAR_BYTES);
// create the jar file
try (JarOutputStream target = new JarOutputStream(tempJarStream)) {
// first, write the manifest file
target.putNextEntry(manifestEntry);
manifest.write(target);
target.closeEntry();
// add the classes
for (String clazz : this.classes.keySet()) {
JarEntry entry = new JarEntry(clazz.replace('.', '/') + ".class");
entry.setLastModifiedTime(timestamp);
entry.setLastAccessTime(timestamp);
entry.setCreationTime(timestamp);
target.putNextEntry(entry);
target.write(this.classes.get(clazz));
target.closeEntry();
}
}
return tempJarStream.toByteArray();
}
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
throws IOException {
Preconditions.checkNotNull(relativePath, "relativePath");
Preconditions.checkNotNull(zos, "zos");
// by JAR spec, if there is a manifest, it must be the first entry in
// the
// ZIP.
File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
if (!manifestFile.exists()) {
zos.putNextEntry(manifestEntry);
new Manifest().write(new BufferedOutputStream(zos));
zos.closeEntry();
} else {
InputStream is = new FileInputStream(manifestFile);
try {
copyToZipStream(is, manifestEntry, zos);
} finally {
if (is != null) {
is.close();
}
if (zos != null) {
zos.closeEntry();
}
}
}
zos.closeEntry();
zipDir(dir, relativePath, zos, true);
zos.close();
}
/**
* Creates a JAR file.
*
* Equivalent to {@code jar cfm <jarfile> <manifest> -C <dir> file...}
*
* The input files are resolved against the given directory. Any input
* files that are directories are processed recursively.
*/
static void createJarFile(Path jarfile, Manifest man, Path dir, String... files)
throws IOException
{
// create the target directory
Path parent = jarfile.getParent();
if (parent != null)
Files.createDirectories(parent);
List<Path> entries = new ArrayList<>();
for (String file : files) {
Files.find(dir.resolve(file), Integer.MAX_VALUE,
(p, attrs) -> attrs.isRegularFile())
.map(e -> dir.relativize(e))
.forEach(entries::add);
}
try (OutputStream out = Files.newOutputStream(jarfile);
JarOutputStream jos = new JarOutputStream(out))
{
if (man != null) {
JarEntry je = new JarEntry(JarFile.MANIFEST_NAME);
jos.putNextEntry(je);
man.write(jos);
jos.closeEntry();
}
for (Path entry : entries) {
String name = toJarEntryName(entry);
jos.putNextEntry(new JarEntry(name));
Files.copy(dir.resolve(entry), jos);
jos.closeEntry();
}
}
}
/**
* {@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;
}
}
ManifestFinder() {
this.spliceJarFilePattern = "/splice_machine";
this.manifestResourcePath = JarFile.MANIFEST_NAME;
}