Java Code Examples for java.util.jar.JarOutputStream#finish()

The following examples show how to use java.util.jar.JarOutputStream#finish() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WLJpa2SwitchSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void createContributionsJar(File jarFile, String classpath) throws IOException {
    //need to create zip file
    OutputStream os = new BufferedOutputStream(new FileOutputStream(jarFile));
    try {
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Name.MANIFEST_VERSION, "1.0"); // NOI18N
        manifest.getMainAttributes().put(Name.CLASS_PATH, classpath);
        JarOutputStream dest = new JarOutputStream(new BufferedOutputStream(os), manifest);
        try {
            dest.closeEntry();
            dest.finish();
        } finally {
            dest.close();
        }
    } finally {
        os.close();
    }
}
 
Example 2
Source File: FindBugsLauncher.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * The minimum requirement to have a "valid" archive plugin is to include
 * findbugs.xml, messages.xml and MANIFEST.MF files. The rest of the
 * resources are load using the parent ClassLoader (Not requires to be in
 * the jar).
 * <p>
 * Instead of building a file on disk, the result of the stream is kept in
 * memory and return as a byte array.
 *
 * @return
 * @throws IOException
 * @throws URISyntaxException 
 */
private byte[] buildFakePluginJar() throws IOException, URISyntaxException {
    ClassLoader cl = getClass().getClassLoader();

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    JarOutputStream jar = new JarOutputStream(buffer);

    final URL metadata = cl.getResource("metadata");
    if (metadata != null) {
        final File dir = new File(metadata.toURI());
        
        //Add files to the jar stream
        addFilesToStream(cl, jar, dir, "");
    }
    jar.finish();
    jar.close();

    return buffer.toByteArray();
}
 
Example 3
Source File: ClassPathResolverTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testManifestClassPathResolver() throws Exception
{
  String artifactId = "hadoop-common";
  Properties properties = new Properties();
  properties.load(this.getClass().getResourceAsStream("/META-INF/maven/org.apache.hadoop/" + artifactId + "/pom.properties"));
  String version = properties.getProperty("version");

  String jarPath = "org/apache/hadoop/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar";

  Manifest manifest = new Manifest();
  manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
  manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarPath);

  File testFile = new File("target/resolverTest.jar");

  FileOutputStream stream = new FileOutputStream(testFile);
  JarOutputStream out = new JarOutputStream(stream, manifest);
  out.putNextEntry(new JarEntry("foo/bar"));
  out.write("Hello World".getBytes());
  out.closeEntry();
  out.finish();
  out.close();

  JarFile jarFile = new JarFile(testFile);
  ClassPathResolvers.JarFileContext jfc = new ClassPathResolvers.JarFileContext(jarFile, new StringWriter());

  ClassPathResolvers cpr = new ClassPathResolvers();
  String baseDir = System.getProperty("localRepository", System.getProperty("user.home") + "/.m2/repository");
  List<ClassPathResolvers.Resolver> resolvers = cpr.createResolvers(ClassPathResolvers.SCHEME_MANIFEST + ":" + baseDir);

  Assert.assertEquals(1, resolvers.size());
  resolvers.get(0).resolve(jfc);

  jarFile.close();

  Assert.assertEquals("number path components " + jfc.urls, 1, jfc.urls.size());
  URL first = jfc.urls.iterator().next();
  Assert.assertEquals("first url", baseDir + "/" + jarPath, first.getPath());
}
 
Example 4
Source File: VirtualFile.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public void createJar(OutputStream outputStream) {
	try {
		JarOutputStream jarOutputStream = new JarOutputStream(outputStream);
		createJar(jarOutputStream);
		jarOutputStream.finish();
	} catch (IOException e) {
		LOGGER.error("", e);
	}
}
 
Example 5
Source File: Main.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                     outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    context.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return false;
    }

    return true;
}
 
Example 6
Source File: Main.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                     outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    context.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return false;
    }

    return true;
}
 
Example 7
Source File: Main.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private static boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                    outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    DxConsole.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            DxConsole.err.println("\ntrouble writing output:");
            ex.printStackTrace(DxConsole.err);
        } else {
            DxConsole.err.println("\ntrouble writing output: " +
                    ex.getMessage());
        }
        return false;
    }

    return true;
}
 
Example 8
Source File: Main.java    From atlas with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private  boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                     outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    DxConsole.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            DxConsole.err.println("\ntrouble writing output:");
            ex.printStackTrace(DxConsole.err);
        } else {
            DxConsole.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return false;
    }

    return true;
}
 
Example 9
Source File: Main.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private static boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                    outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    DxConsole.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            DxConsole.err.println("\ntrouble writing output:");
            ex.printStackTrace(DxConsole.err);
        } else {
            DxConsole.err.println("\ntrouble writing output: " +
                    ex.getMessage());
        }
        return false;
    }

    return true;
}
 
Example 10
Source File: Main.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                     outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    context.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                               ex.getMessage());
        }
        return false;
    }

    return true;
}
 
Example 11
Source File: Main.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a jar file from the resources (including dex file arrays).
 *
 * @param fileName {@code non-null;} name of the file
 * @return whether the creation was successful
 */
private boolean createJar(String fileName) {
    /*
     * Make or modify the manifest (as appropriate), put the dex
     * array into the resources map, and then process the entire
     * resources map in a uniform manner.
     */

    try {
        Manifest manifest = makeManifest();
        OutputStream out = openOutput(fileName);
        JarOutputStream jarOut = new JarOutputStream(out, manifest);

        try {
            for (Map.Entry<String, byte[]> e :
                outputResources.entrySet()) {
                String name = e.getKey();
                byte[] contents = e.getValue();
                JarEntry entry = new JarEntry(name);
                int length = contents.length;

                if (args.verbose) {
                    context.out.println("writing " + name + "; size " + length + "...");
                }

                entry.setSize(length);
                jarOut.putNextEntry(entry);
                jarOut.write(contents);
                jarOut.closeEntry();
            }
        } finally {
            jarOut.finish();
            jarOut.flush();
            closeOutput(out);
        }
    } catch (Exception ex) {
        if (args.debug) {
            context.err.println("\ntrouble writing output:");
            ex.printStackTrace(context.err);
        } else {
            context.err.println("\ntrouble writing output: " +
                ex.getMessage());
        }
        return false;
    }

    return true;
}