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) throws IOException 

Source Link

Document

Creates a new JarOutputStream with no manifest.

Usage

From source file:interactivespaces.workbench.project.java.BndOsgiContainerBundleCreator.java

/**
 * Create a Jar file from a source folder.
 *
 * @param jarDestinationFile/*from w  ww . j  a  v a  2s  .c  o  m*/
 *          the jar file being created
 * @param sourceFolder
 *          the source folder containing the classes
 */
private void createJarFile(File jarDestinationFile, File sourceFolder) {
    // Create a buffer for reading the files
    byte[] buf = new byte[IO_BUFFER_SIZE];

    JarOutputStream out = null;
    try {
        // Create the jar file
        out = new JarOutputStream(new FileOutputStream(jarDestinationFile));

        writeJarFile(sourceFolder, buf, out, "");

        // Complete the jar file
        out.close();
    } catch (Exception e) {
        throw new InteractiveSpacesException(
                String.format("Failed creating jar file %s", jarDestinationFile.getAbsolutePath()), e);
    } finally {
        fileSupport.close(out, true);
    }
}

From source file:org.silverpeas.applicationbuilder.WriteOnlyArchive.java

private void setOutputStream() throws AppBuilderException {
    try {// w  ww.  jav  a  2 s  .c om
        OutputStream out = new FileOutputStream(getPath().getAbsolutePath());
        jarOut = new JarOutputStream(out);
        jarOut.setMethod(JarOutputStream.DEFLATED);
    } catch (Exception e) {
        throw new AppBuilderException(getPath().getAbsolutePath() + " : impossible to create", e);
    }
}

From source file:org.apache.hadoop.mapred.TestTaskTrackerLocalization.java

/**
 * @param jobConf//from   w w  w .  j av a2s .c o  m
 * @throws IOException
 * @throws FileNotFoundException
 */
private void uploadJobJar(JobConf jobConf) throws IOException, FileNotFoundException {
    File jobJarFile = new File(TEST_ROOT_DIR, "jobjar-on-dfs.jar");
    JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jobJarFile));
    ZipEntry ze = new ZipEntry("lib/lib1.jar");
    jstream.putNextEntry(ze);
    jstream.closeEntry();
    ze = new ZipEntry("lib/lib2.jar");
    jstream.putNextEntry(ze);
    jstream.closeEntry();
    jstream.finish();
    jstream.close();
    jobConf.setJar(jobJarFile.toURI().toString());
}

From source file:de.tobiasroeser.maven.featurebuilder.FeatureBuilder.java

private boolean createFeatureJar(final String featureId, final String featureVersion, final String featureXml,
        final String jarDir) {

    final File file = new File(jarDir, featureId + "_" + featureVersion + ".jar");
    file.getParentFile().mkdirs();// www  .ja  va  2  s  .  co m

    try {
        final JarOutputStream jarOutputStream = new JarOutputStream(
                new BufferedOutputStream(new FileOutputStream(file)));

        final JarEntry entry = new JarEntry("feature.xml");
        jarOutputStream.putNextEntry(entry);

        final BufferedInputStream featureXmlStream = new BufferedInputStream(new FileInputStream(featureXml));
        copy(featureXmlStream, jarOutputStream);

        jarOutputStream.close();

    } catch (final FileNotFoundException e) {
        throw new RuntimeException("Could not create Feature Jar: " + file.getAbsolutePath(), e);
    } catch (final IOException e) {
        throw new RuntimeException("Could not create Feature Jar: " + file.getAbsolutePath(), e);
    }

    return true;
}

From source file:com.glaf.core.util.ZipUtils.java

public static byte[] getZipBytes(Map<String, InputStream> dataMap) {
    byte[] bytes = null;
    try {/*from ww  w. j  av  a 2  s.  co  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        JarOutputStream jos = new JarOutputStream(bos);
        if (dataMap != null) {
            Set<Entry<String, InputStream>> entrySet = dataMap.entrySet();
            for (Entry<String, InputStream> entry : entrySet) {
                String name = entry.getKey();
                InputStream inputStream = entry.getValue();
                if (name != null && inputStream != null) {
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    JarEntry jarEntry = new JarEntry(name);
                    jos.putNextEntry(jarEntry);

                    while ((len = bis.read(buf)) >= 0) {
                        jos.write(buf, 0, len);
                    }

                    bis.close();
                    jos.closeEntry();
                }
            }
        }
        jos.flush();
        jos.close();
        bos.flush();
        bos.close();
        bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.codehaus.mojo.cassandra.AbstractCassandraMojo.java

/**
 * Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for
 * all classpath elements. Copied from surefire (ForkConfiguration#createJar())
 *
 * @param jarFile   The jar file to create/update
 * @param mainClass The main class to run.
 * @throws java.io.IOException if something went wrong.
 *///from ww w  .  j a v  a  2s  . co  m
protected void createCassandraJar(File jarFile, String mainClass, File cassandraDir) throws IOException {
    File conf = new File(cassandraDir, "conf");
    FileOutputStream fos = null;
    JarOutputStream jos = null;
    try {
        fos = new FileOutputStream(jarFile);
        jos = new JarOutputStream(fos);
        jos.setLevel(JarOutputStream.STORED);
        jos.putNextEntry(new JarEntry("META-INF/MANIFEST.MF"));

        Manifest man = new Manifest();

        // we can't use StringUtils.join here since we need to add a '/' to
        // the end of directory entries - otherwise the jvm will ignore them.
        StringBuilder cp = new StringBuilder();
        cp.append(new URL(conf.toURI().toASCIIString()).toExternalForm());
        cp.append(' ');
        getLog().debug("Adding plugin artifact: " + ArtifactUtils.versionlessKey(pluginArtifact)
                + " to the classpath");
        cp.append(new URL(pluginArtifact.getFile().toURI().toASCIIString()).toExternalForm());
        cp.append(' ');

        for (Artifact artifact : this.pluginDependencies) {
            getLog().debug("Adding plugin dependency artifact: " + ArtifactUtils.versionlessKey(artifact)
                    + " to the classpath");
            // NOTE: if File points to a directory, this entry MUST end in '/'.
            cp.append(new URL(artifact.getFile().toURI().toASCIIString()).toExternalForm());
            cp.append(' ');
        }

        if (addMainClasspath || addTestClasspath) {
            if (addTestClasspath) {
                getLog().debug("Adding: " + testClassesDirectory + " to the classpath");
                cp.append(new URL(testClassesDirectory.toURI().toASCIIString()).toExternalForm());
                cp.append(' ');
            }
            if (addMainClasspath) {
                getLog().debug("Adding: " + classesDirectory + " to the classpath");
                cp.append(new URL(classesDirectory.toURI().toASCIIString()).toExternalForm());
                cp.append(' ');
            }
            for (Artifact artifact : (Set<Artifact>) this.project.getArtifacts()) {
                if ("jar".equals(artifact.getType()) && !Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
                        && (!Artifact.SCOPE_TEST.equals(artifact.getScope()) || addTestClasspath)) {
                    getLog().debug("Adding dependency: " + ArtifactUtils.versionlessKey(artifact)
                            + " to the classpath");
                    // NOTE: if File points to a directory, this entry MUST end in '/'.
                    cp.append(new URL(artifact.getFile().toURI().toASCIIString()).toExternalForm());
                    cp.append(' ');
                }
            }
        }

        man.getMainAttributes().putValue("Manifest-Version", "1.0");
        man.getMainAttributes().putValue("Class-Path", cp.toString().trim());
        man.getMainAttributes().putValue("Main-Class", mainClass);

        man.write(jos);
    } finally {
        IOUtil.close(jos);
        IOUtil.close(fos);
    }
}

From source file:org.apache.solr.core.TestCoreContainer.java

@Test
public void testSharedLib() throws Exception {
    Path tmpRoot = createTempDir("testSharedLib");

    File lib = new File(tmpRoot.toFile(), "lib");
    lib.mkdirs();/* w  w  w .j  av a2 s  . com*/

    JarOutputStream jar1 = new JarOutputStream(new FileOutputStream(new File(lib, "jar1.jar")));
    jar1.putNextEntry(new JarEntry("defaultSharedLibFile"));
    jar1.closeEntry();
    jar1.close();

    File customLib = new File(tmpRoot.toFile(), "customLib");
    customLib.mkdirs();

    JarOutputStream jar2 = new JarOutputStream(new FileOutputStream(new File(customLib, "jar2.jar")));
    jar2.putNextEntry(new JarEntry("customSharedLibFile"));
    jar2.closeEntry();
    jar2.close();

    final CoreContainer cc1 = init(tmpRoot, "<solr></solr>");
    try {
        cc1.loader.openResource("defaultSharedLibFile").close();
    } finally {
        cc1.shutdown();
    }

    final CoreContainer cc2 = init(tmpRoot, "<solr><str name=\"sharedLib\">lib</str></solr>");
    try {
        cc2.loader.openResource("defaultSharedLibFile").close();
    } finally {
        cc2.shutdown();
    }

    final CoreContainer cc3 = init(tmpRoot, "<solr><str name=\"sharedLib\">customLib</str></solr>");
    try {
        cc3.loader.openResource("customSharedLibFile").close();
    } finally {
        cc3.shutdown();
    }
}

From source file:JarMaker.java

/**
 * Combine several jar files and some given files into one jar file.
 * @param outJar The output jar file's filename.
 * @param inJarsList The jar files to be combined
 * @param filter A filter that exclude some entries of input jars. User
 *        should implement the EntryFilter interface.
 * @param inFileList The files to be added into the jar file.
 * @param prefixs The prefixs of files to be added into the jar file.
 *        inFileList and prefixs should be paired.
 * @throws FileNotFoundException/*from w w w.j ava2s .  c  om*/
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList,
        String[] prefixs) throws FileNotFoundException, IOException {

    JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar));
    ArrayList entryList = new ArrayList();
    for (int i = 0; i < inJarsList.length; i++) {
        BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i]));
        ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
        byte abyte0[] = new byte[1024 * 4];
        for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
                .getNextEntry()) {
            if (filter.accept(zipentry)) {
                if (!entryList.contains(zipentry.getName())) {
                    jout.putNextEntry(zipentry);
                    if (!zipentry.isDirectory()) {
                        int j;
                        try {
                            while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                                jout.write(abyte0, 0, j);
                        } catch (IOException ie) {
                            throw ie;
                        }
                    }
                    entryList.add(zipentry.getName());
                }
            }
        }
        zipinputstream.close();
        bufferedinputstream.close();
    }
    for (int i = 0; i < inFileList.length; i++) {
        add(jout, new File(inFileList[i]), prefixs[i]);
    }
    jout.close();
}

From source file:com.germinus.easyconf.FileUtil.java

public static void writeAsJAR(File dest, String propsFileName, Properties props)
        throws FileNotFoundException, IOException {
    JarOutputStream out = new JarOutputStream(new FileOutputStream(dest));
    JarEntry propertiesFile = new JarEntry(propsFileName);
    propertiesFile.setExtra(propertiesToString(props).getBytes());
    out.putNextEntry(propertiesFile);/* w w  w . j a v  a 2s . c  o m*/
    out.close();
}

From source file:io.smartspaces.workbench.project.java.BndOsgiContainerBundleCreator.java

/**
 * Create a Jar file from a source folder.
 *
 * @param jarDestinationFile/*from  ww w.j  av a  2s .  c  o m*/
 *          the jar file being created
 * @param sourceFolder
 *          the source folder containing the classes
 */
private void createJarFile(File jarDestinationFile, File sourceFolder) {
    // Create a buffer for reading the files
    byte[] buf = new byte[IO_BUFFER_SIZE];

    JarOutputStream out = null;
    try {
        // Create the jar file
        out = new JarOutputStream(new FileOutputStream(jarDestinationFile));

        writeJarFile(sourceFolder, buf, out, "");

        // Complete the jar file
        out.close();
    } catch (Exception e) {
        throw new SmartSpacesException(
                String.format("Failed creating jar file %s", jarDestinationFile.getAbsolutePath()), e);
    } finally {
        fileSupport.close(out, true);
    }
}