Example usage for java.util.jar JarOutputStream JarOutputStream

List of usage examples for java.util.jar JarOutputStream JarOutputStream

Introduction

In this page you can find the example usage for java.util.jar JarOutputStream JarOutputStream.

Prototype

public JarOutputStream(OutputStream out, Manifest man) throws IOException 

Source Link

Document

Creates a new JarOutputStream with the specified Manifest.

Usage

From source file:JarBuilder.java

/** Creates a file file without a manifest
 *
 * @param file the file to write the jar to
 * @throws IOException thrown if the file cannot be opened for writing
 *//* ww w  . j  av a2  s.  c  o m*/
public JarBuilder(File file) throws IOException {
    _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(file)), ManifestWriter.DEFAULT);
}

From source file:org.hecl.jarhack.JarHack.java

/**
 * The <code>substHecl</code> method takes the filenames of two
 * .jar's - one as input, the second as output, in addition to the
 * name of the application.  Where it counts, the old name (Hecl,
 * usually) is overridden with the new name, and the new .jar file
 * is written to the specified outfile.  Via the iconname argument
 * it is also possible to specify a new icon file to use.
 *
 * @param infile a <code>FileInputStream</code> value
 * @param outfile a <code>String</code> value
 * @param newname a <code>String</code> value
 * @param iconname a <code>String</code> value
 * @exception IOException if an error occurs
 *//*ww  w  .  jav  a 2 s  .  c om*/
public static void substHecl(InputStream infile, String outfile, String newname, String iconname,
        String scriptfile) throws IOException {

    JarInputStream jif = new JarInputStream(infile);
    Manifest mf = jif.getManifest();
    Attributes attrs = mf.getMainAttributes();

    Set keys = attrs.keySet();
    Iterator it = keys.iterator();
    while (it.hasNext()) {
        Object key = it.next();
        Object value = attrs.get(key);
        String keyname = key.toString();

        /* These are the three cases that interest us in
         * particular, where we need to make changes. */
        if (keyname.equals("MIDlet-Name")) {
            attrs.putValue(keyname, newname);
        } else if (keyname.equals("MIDlet-1")) {
            String valuestr = value.toString();
            /* FIXME - the stringsplit method is used for older
             * versions of GCJ.  Once newer versions are common,
             * it can go away.  Or not - it works just fine. */
            String properties[] = stringsplit(valuestr, ", ");
            attrs.putValue(keyname, newname + ", " + properties[1] + ", " + properties[2]);
        } else if (keyname.equals("MicroEdition-Configuration")) {
            cldcversion = value.toString();
        } else if (keyname.equals("MicroEdition-Profile")) {
            midpversion = value.toString();
        } else if (keyname.equals("MIDlet-Jar-URL")) {
            attrs.put(key, newname + ".jar");
        }
    }

    JarOutputStream jof = new JarOutputStream(new FileOutputStream(outfile), mf);

    byte[] buf = new byte[4096];

    /* Go through the various entries. */
    JarEntry entry;
    int read;
    while ((entry = jif.getNextJarEntry()) != null) {

        /* Don't copy the manifest file. */
        if ("META-INF/MANIFEST.MF".equals(entry.getName()))
            continue;

        /* Insert our own icon */
        if (iconname != null && "Hecl.png".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("Hecl.png"));
            FileInputStream inf = new FileInputStream(iconname);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        }
        /* Insert our own copy of the script file. */
        else if ("script.hcl".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("script.hcl"));
            FileInputStream inf = new FileInputStream(scriptfile);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        } else {
            /* Otherwise, just copy the entry. */
            jof.putNextEntry(entry);
            while ((read = jif.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
        }

        jof.closeEntry();
    }

    jof.flush();
    jof.close();
    jif.close();
}

From source file:gov.redhawk.rap.internal.PluginProviderServlet.java

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    final Path path = new Path(req.getRequestURI());
    String pluginId = path.lastSegment();
    if (pluginId.endsWith(".jar")) {
        pluginId = pluginId.substring(0, pluginId.length() - 4);
    }//from www  . j ava2  s .c o  m
    final Bundle b = Platform.getBundle(pluginId);
    if (b == null) {
        final String protocol = req.getProtocol();
        final String msg = "Plugin does not exist: " + pluginId;
        if (protocol.endsWith("1.1")) {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
        } else {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        }
        return;
    }
    final File file = FileLocator.getBundleFile(b);
    resp.setContentType("application/octet-stream");

    if (file.isFile()) {
        final String contentDisposition = "attachment; filename=\"" + file.getName() + "\"";
        resp.setHeader("Content-Disposition", contentDisposition);
        resp.setContentLength((int) file.length());
        final FileInputStream istream = new FileInputStream(file);
        try {
            IOUtils.copy(istream, resp.getOutputStream());
        } finally {
            istream.close();
        }
        resp.flushBuffer();
    } else {
        final String contentDisposition = "attachment; filename=\"" + file.getName() + ".jar\"";
        resp.setHeader("Content-Disposition", contentDisposition);
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        final Manifest man = new Manifest(new FileInputStream(new File(file, "META-INF/MANIFEST.MF")));
        final JarOutputStream out = new JarOutputStream(outputStream, man);
        final File binDir = new File(file, "bin");

        if (binDir.exists()) {
            addFiles(out, Path.ROOT, binDir.listFiles());
            for (final File f : file.listFiles()) {
                if (!f.getName().equals("bin")) {
                    addFiles(out, Path.ROOT, f);
                }
            }
        } else {
            addFiles(out, Path.ROOT, file.listFiles());
        }
        out.close();
        outputStream.close();
        final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        resp.setContentLength(outputStream.size());
        try {
            IOUtils.copy(inputStream, resp.getOutputStream());
        } finally {
            inputStream.close();
        }
        resp.flushBuffer();
    }

}

From source file:org.sonar.api.utils.ManifestUtilsTest.java

private File createJar(Manifest mf, String name) throws Exception {
    mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    File file = tempDir.newFile(name);
    OutputStream out = new JarOutputStream(new FileOutputStream(file), mf);
    out.flush();//from ww w .j ava 2  s .c o m
    IOUtils.closeQuietly(out);
    return file;
}

From source file:JarBuilder.java

/** Creates an empty jar file with the given manifest
 *
 * @param jar      the file to write the jar to
 * @param manifest the file that is the manifest for the archive
 * @throws IOException thrown if either file cannot be opened for reading
 *//*from  w  w w. j  a v  a 2 s  .  co m*/
public JarBuilder(File jar, File manifest) throws IOException {
    _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar)),
            new Manifest(new FileInputStream(manifest)));
}

From source file:org.reficio.p2.FeatureBuilder.java

public void generate(File destinationFolder) {
    try {//w  w w .  j  a  v  a2 s  .  co m
        File featureContent = new File(destinationFolder, this.getFeatureFullName());
        featureContent.mkdir();
        this.buildXml();
        XmlUtils.writeXml(this.xmlDoc, new File(featureContent, "feature.xml"));

        //we must be generating the feature file from the pom
        FileOutputStream fos = new FileOutputStream(
                new File(destinationFolder, this.getFeatureFullName() + ".jar"));
        Manifest mf = new Manifest();
        JarOutputStream jar = new JarOutputStream(fos, mf);
        addToJar(jar, featureContent);

    } catch (Exception e) {
        throw new RuntimeException("Cannot generate feature", e);
    }
}

From source file:com.aliyun.odps.mapred.BridgeJobRunner.java

/**
 * Create jar with jobconf./* w w  w  .j ava2s. c  om*/
 *
 * @return
 * @throws OdpsException
 */
private ByteArrayOutputStream createJarArchive() throws OdpsException {
    try {
        ByteArrayOutputStream archiveOut = new ByteArrayOutputStream();
        // Open archive file
        JarOutputStream out = new JarOutputStream(archiveOut, new Manifest());

        ByteArrayOutputStream jobOut = new ByteArrayOutputStream();
        job.writeXml(jobOut);
        // Add jobconf entry
        JarEntry jobconfEntry = new JarEntry("jobconf.xml");
        out.putNextEntry(jobconfEntry);
        out.write(jobOut.toByteArray());

        out.close();
        return archiveOut;
    } catch (IOException ex) {
        throw new OdpsException(ErrorCode.UNEXPECTED.toString(), ex);
    }
}

From source file:com.navercorp.pinpoint.bootstrap.AgentDirGenerator.java

private void createJarFile(File parentDir, String filepath) throws IOException {
    final String jarPath = parentDir.getPath() + File.separator + filepath;
    logger.debug("create jar:{}", jarPath);

    JarOutputStream jos = null;//w  w  w .  j av  a2 s.c om
    try {
        Manifest manifest = new Manifest();
        FileOutputStream out = new FileOutputStream(jarPath);
        jos = new JarOutputStream(out, manifest);
    } finally {
        IOUtils.closeQuietly(jos);
    }

}

From source file:com.asual.summer.onejar.OneJarMojo.java

public void execute() throws MojoExecutionException {

    JarOutputStream out = null;/*w  ww  .  ja va 2s  . c o m*/
    JarInputStream in = null;

    try {

        File jarFile = new File(buildDirectory, jarName);
        File warFile = new File(buildDirectory, warName);

        Manifest manifest = new Manifest(new ByteArrayInputStream("Manifest-Version: 1.0\n".getBytes("UTF-8")));
        manifest.getMainAttributes().putValue("Main-Class", JAR_MAIN_CLASS);
        manifest.getMainAttributes().putValue("One-Jar-Main-Class", ONE_JAR_MAIN_CLASS);

        out = new JarOutputStream(new FileOutputStream(jarFile, false), manifest);
        in = new JarInputStream(getClass().getClassLoader().getResourceAsStream(ONE_JAR_DIST));

        putEntry(out, new FileInputStream(warFile), new ZipEntry(JAR_CLASSPATH + warFile.getName()));

        for (Artifact artifact : pluginArtifacts) {
            if (artifact.getArtifactId().equalsIgnoreCase("summer-onejar")) {
                artifact.updateVersion(artifact.getVersion(), localRepository);
                putEntry(out, new FileInputStream(artifact.getFile()),
                        new ZipEntry(JAR_CLASSPATH + artifact.getFile().getName()));
                MavenProject project = mavenProjectBuilder.buildFromRepository(artifact,
                        remoteArtifactRepositories, localRepository);
                List<Dependency> dependencies = project.getDependencies();
                for (Dependency dependency : dependencies) {
                    if (!"provided".equals(dependency.getScope())) {
                        Artifact dependencyArtifact = artifactFactory.createArtifact(dependency.getGroupId(),
                                dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(),
                                dependency.getType());
                        dependencyArtifact.updateVersion(dependencyArtifact.getVersion(), localRepository);
                        putEntry(out, new FileInputStream(dependencyArtifact.getFile()),
                                new ZipEntry(JAR_CLASSPATH + dependencyArtifact.getFile().getName()));
                    }
                }
            }
        }

        while (true) {
            ZipEntry entry = in.getNextEntry();
            if (entry != null) {
                putEntry(out, in, entry);
            } else {
                break;
            }
        }

        projectHelper.attachArtifact(project, "jar", jarFile);

    } catch (Exception e) {
        getLog().error(e.getMessage(), e);
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:JarBuilder.java

/** Creates an empty jar file with the given manifest
 *
 * @param jar      the file to write the jar to
 * @param manifest the manifest file for the jar
 * @see ManifestWriter/*from ww w .  j a  v a2  s .  c o  m*/
 */
public JarBuilder(File jar, Manifest manifest) {
    try {
        _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar)), manifest);
    } catch (IOException e) {
        e.printStackTrace();
    }
}