Example usage for java.util.jar JarOutputStream setLevel

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

Introduction

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

Prototype

public void setLevel(int level) 

Source Link

Document

Sets the compression level for subsequent entries which are DEFLATED.

Usage

From source file:ch.randelshofer.cubetwister.HTMLExporter.java

/**
 * Processes a pack200 template, by writing it both as a pack200 file and
 * a Jar file to the output stream.//from  w  w  w  . j a v  a 2s  . c o m
 *
 * @param filename The name of the template. Must end with ".jar.pack.gz".
 * @param in An input stream for reading the contents of the template.
 */
private void processPack200Template(String filename, InputStream in) throws IOException {
    p.setNote("Exporting " + filename + " ...");
    p.setProgress(p.getProgress() + 1);

    // Determine whether we can skip this file
    boolean skip = true;
    int pos1 = filename.lastIndexOf('/');
    int pos2 = filename.lastIndexOf("Player.jar.pack.gz");
    if (pos2 != -1) {
        for (CubeKind ck : playerCubeKinds) {
            if (ck.isNameOfKind(filename.substring(pos1 + 1, pos2))) {
                skip = false;
                break;
            }
        }
        if (skip)
            for (CubeKind ck : virtualCubeKinds) {
                if (ck.isNameOfKind(filename.substring(pos1 + 1, pos2))) {
                    skip = false;
                    break;
                }
            }
    }
    if (skip) {
        pos1 = filename.lastIndexOf("Virtual");
        pos2 = filename.lastIndexOf(".jar.pack.gz");
        if (pos2 != -1) {
            for (CubeKind ck : virtualCubeKinds) {
                if (ck.isNameOfKind(filename.substring(pos1 + 7, pos2))) {
                    skip = false;
                    break;
                }
            }
        }
    }
    if (skip) {
        return;
    }

    byte[] buf = new byte[1024];
    int len;

    // Write the pack200 file into the output and into a temporary buffer
    putNextEntry(filename);
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    while (-1 != (len = in.read(buf, 0, buf.length))) {
        tmp.write(buf, 0, len);
        entryOut.write(buf, 0, len);
    }
    closeEntry();
    tmp.close();

    // Uncompress the pack200 file from the temporary buffer into the output
    putNextEntry(filename.substring(0, filename.length() - 8));
    InputStream itmp = new GZIPInputStream(new ByteArrayInputStream(tmp.toByteArray()));
    JarOutputStream jout = new JarOutputStream(entryOut);
    jout.setLevel(Deflater.BEST_COMPRESSION);
    Unpacker unpacker = Pack200.newUnpacker();
    unpacker.unpack(itmp, jout);

    jout.finish();
    closeEntry();
}

From source file:com.taobao.android.TPatchTool.java

/**
 * ?so/*w  w w.  j av a2s. c o  m*/
 *
 * @param toZipFolder
 * @param soOutputFile
 */
private void zipBundle(File toZipFolder, File soOutputFile) throws IOException {
    FileOutputStream fileOutputStream = null;
    JarOutputStream jos = null;
    try {
        // ?so
        Manifest manifest = createManifest();
        fileOutputStream = new FileOutputStream(soOutputFile);
        jos = new JarOutputStream(new BufferedOutputStream(fileOutputStream), manifest);
        jos.setLevel(9);
        //            jos.setComment(baseApkVersion+"@"+newApkVersion);
        // Add ZIP entry to output stream.
        File[] files = toZipFolder.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                addDirectory(jos, file, file.getName());
            } else {
                addFile(jos, file);
            }
        }
    } finally {
        IOUtils.closeQuietly(jos);
        if (null != fileOutputStream) {
            IOUtils.closeQuietly(fileOutputStream);
        }
    }
}

From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java

/**
 * Change the version in jar/*  www  .ja  va2  s . c o m*/
 * 
 * @param newVersion
 * @param file
 * @return
 * @throws MojoExecutionException
 */
protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException {
    String fileName = file.getName();
    int pos = fileName.indexOf(oldVersion);
    fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length());

    JarInputStream jis = null;
    JarOutputStream jos;
    OutputStream out = null;
    JarFile sourceJar = null;
    try {
        // now create a temporary file and update the version
        sourceJar = new JarFile(file);
        final Manifest manifest = sourceJar.getManifest();
        manifest.getMainAttributes().putValue("Bundle-Version", newVersion);

        jis = new JarInputStream(new FileInputStream(file));
        final File destJar = new File(file.getParentFile(), fileName);
        out = new FileOutputStream(destJar);
        jos = new JarOutputStream(out, manifest);

        jos.setMethod(JarOutputStream.DEFLATED);
        jos.setLevel(Deflater.BEST_COMPRESSION);

        JarEntry entryIn = jis.getNextJarEntry();
        while (entryIn != null) {
            JarEntry entryOut = new JarEntry(entryIn.getName());
            entryOut.setTime(entryIn.getTime());
            entryOut.setComment(entryIn.getComment());
            jos.putNextEntry(entryOut);
            if (!entryIn.isDirectory()) {
                IOUtils.copy(jis, jos);
            }
            jos.closeEntry();
            jis.closeEntry();
            entryIn = jis.getNextJarEntry();
        }

        // close the JAR file now to force writing
        jos.close();
        return destJar;
    } catch (IOException ioe) {
        throw new MojoExecutionException("Unable to update version in jar file.", ioe);
    } finally {
        if (sourceJar != null) {
            try {
                sourceJar.close();
            } catch (IOException ex) {
                // close
            }
        }
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(out);
    }

}

From source file:org.apache.sling.osgi.obr.Repository.java

File spoolModified(InputStream ins) throws IOException {
    JarInputStream jis = new JarInputStream(ins);

    // immediately handle the manifest
    JarOutputStream jos;
    Manifest manifest = jis.getManifest();
    if (manifest == null) {
        throw new IOException("Missing Manifest !");
    }//w w w. j  a  v  a2  s  . c  o  m

    String symbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName");
    if (symbolicName == null || symbolicName.length() == 0) {
        throw new IOException("Missing Symbolic Name in Manifest !");
    }

    String version = manifest.getMainAttributes().getValue("Bundle-Version");
    Version v = Version.parseVersion(version);
    if (v.getQualifier().indexOf("SNAPSHOT") >= 0) {
        String tStamp;
        synchronized (DATE_FORMAT) {
            tStamp = DATE_FORMAT.format(new Date());
        }
        version = v.getMajor() + "." + v.getMinor() + "." + v.getMicro() + "."
                + v.getQualifier().replaceAll("SNAPSHOT", tStamp);
        manifest.getMainAttributes().putValue("Bundle-Version", version);
    }

    File bundle = new File(this.repoLocation, symbolicName + "-" + v + ".jar");
    OutputStream out = null;
    try {
        out = new FileOutputStream(bundle);
        jos = new JarOutputStream(out, manifest);

        jos.setMethod(JarOutputStream.DEFLATED);
        jos.setLevel(Deflater.BEST_COMPRESSION);

        JarEntry entryIn = jis.getNextJarEntry();
        while (entryIn != null) {
            JarEntry entryOut = new JarEntry(entryIn.getName());
            entryOut.setTime(entryIn.getTime());
            entryOut.setComment(entryIn.getComment());
            jos.putNextEntry(entryOut);
            if (!entryIn.isDirectory()) {
                spool(jis, jos);
            }
            jos.closeEntry();
            jis.closeEntry();
            entryIn = jis.getNextJarEntry();
        }

        // close the JAR file now to force writing
        jos.close();

    } finally {
        IOUtils.closeQuietly(out);
    }

    return bundle;
}

From source file:org.apache.sling.tooling.support.install.impl.InstallServlet.java

private static void createJar(final File sourceDir, final File jarFile, final Manifest mf) throws IOException {
    final JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
    try {/* w  w w  . jav a 2 s. c o  m*/
        zos.setLevel(Deflater.NO_COMPRESSION);
        // manifest first
        final ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
        zos.putNextEntry(anEntry);
        mf.write(zos);
        zos.closeEntry();
        zipDir(sourceDir, zos, "");
    } finally {
        try {
            zos.close();
        } catch (final IOException ignore) {
            // ignore
        }
    }
}

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.
 *//*  w ww  . jav a 2  s  . 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.stem.ExternalNode.java

private void createNodeJar(File jarFile, String mainClass, File nodeDir) throws IOException {
    File conf = new File(nodeDir, "conf");

    FileOutputStream fos = null;//from ww w  .ja v  a2  s. c o  m
    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();

        StringBuilder cp = new StringBuilder();
        cp.append(new URL(conf.toURI().toASCIIString()).toExternalForm());
        cp.append(' ');

        log.debug("Adding plugin artifact: " + ArtifactUtils.versionlessKey(mvnContext.pluginArtifact)
                + " to the classpath");
        cp.append(new URL(mvnContext.pluginArtifact.getFile().toURI().toASCIIString()).toExternalForm());
        cp.append(' ');

        log.debug("Adding: " + mvnContext.classesDir + " to the classpath");
        cp.append(new URL(mvnContext.classesDir.toURI().toASCIIString()).toExternalForm());
        cp.append(' ');

        for (Artifact artifact : mvnContext.pluginDependencies) {
            log.info("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(' ');
        }

        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);
    }
}