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:JarUtil.java

/**
 * Writes all given files to the specified jar-file.
 * // ww w .  j a v  a 2 s  .c  o  m
 * @param files
 *            all files that should be added to the JAR file
 * @param sourceDir
 *            The parent directory containing the given files.
 * @param target
 *            The jar file which should be created
 * @param compress
 *            True when the jar file should be compressed
 * @throws FileNotFoundException
 *             when a file could not be found
 * @throws IOException
 *             when a file could not be read or the jar file could not be
 *             written to.
 */
public static void jar(File sourceDir, OutputStream target, boolean compress) throws IOException {
    File[] files = sourceDir.listFiles();
    // creates target-jar-file:
    JarOutputStream out = new JarOutputStream(target);
    if (compress) {
        out.setLevel(ZipOutputStream.DEFLATED);
    } else {
        out.setLevel(ZipOutputStream.STORED);
    }
    // create a CRC32 object:
    CRC32 crc = new CRC32();
    byte[] buffer = new byte[1024 * 1024];
    // add all files:
    int sourceDirLength = sourceDir.getAbsolutePath().length() + 1;
    for (File file : files) {
        addFile(file, out, crc, sourceDirLength, buffer);
    }
    out.close();
}

From source file:com.speed.ob.api.ClassStore.java

public void init(JarInputStream jarIn, File output, File in) throws IOException {
    ZipEntry entry;/*from w  ww.  j a va 2 s  .  c  om*/
    JarOutputStream out = new JarOutputStream(new FileOutputStream(new File(output, in.getName())));
    while ((entry = jarIn.getNextEntry()) != null) {
        byte[] data = IOUtils.toByteArray(jarIn);
        if (entry.getName().endsWith(".class")) {
            ClassReader reader = new ClassReader(data);
            ClassNode cn = new ClassNode();
            reader.accept(cn, ClassReader.EXPAND_FRAMES);
            store.put(cn.name, cn);
        } else if (!entry.isDirectory()) {
            Logger.getLogger(getClass().getName()).finer("Storing " + entry.getName() + " in output file");
            JarEntry je = new JarEntry(entry.getName());
            out.putNextEntry(je);
            out.write(data);
            out.closeEntry();
        }
    }
    out.close();
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Updates the version numbers of a plugin.
 * @param tmpFolder A temporary folder where files can be created and deleted.
 * Will be used for unpacking the plugin.
 * @param plugin The location of a plugin (jar file) to transform.
 * @param version The new version number of the plugin.
 * @param destinationFolder The destination folder where the transformed plugin shall be saved to.
 * @throws IOException /*from  w ww.j av  a2  s.  c  o m*/
 */
static void updatePlugins(File tmpFolder, File plugin, String version, File destinationFolder)
        throws IOException {

    unZipIt(plugin, tmpFolder);
    GenericVersionReplacer replacer = new GenericVersionReplacer();
    replacer.setVersion(version);
    replacer.setPath(tmpFolder.getAbsolutePath());
    replacer.execute();

    File destFile = new File(destinationFolder, plugin.getName());
    String basePath = FilenameUtils.normalize(tmpFolder.getAbsolutePath());
    if (!basePath.endsWith(File.separator)) {
        basePath += File.separator;
    }
    try (ZipOutputStream out = new JarOutputStream(new FileOutputStream(destFile))) {
        addDir(basePath, tmpFolder, out);
    }
}

From source file:org.commonjava.indy.ftest.core.content.StoreAndVerifyJarViaDirectDownloadTest.java

@Test
public void storeFileThenDownloadAndVerifyContentViaDirectDownload() throws Exception {
    final String content = "This is a test: " + System.nanoTime();
    String entryName = "org/something/foo.class";

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JarOutputStream jarOut = new JarOutputStream(out);
    jarOut.putNextEntry(new JarEntry(entryName));
    jarOut.write(content.getBytes());/*from   w  ww.j  a v a 2 s . com*/
    jarOut.close();

    // Used to visually inspect the jars moving up...
    //        String userDir = System.getProperty( "user.home" );
    //        File dir = new File( userDir, "temp" );
    //        dir.mkdirs();
    //
    //        FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-in.jar" ), out.toByteArray() );

    final InputStream stream = new ByteArrayInputStream(out.toByteArray());

    final String path = "/path/to/" + getClass().getSimpleName() + "-" + name.getMethodName() + ".jar";

    assertThat(client.content().exists(hosted, STORE, path), equalTo(false));

    client.content().store(hosted, STORE, path, stream);

    assertThat(client.content().exists(hosted, STORE, path), equalTo(true));

    final URL url = new URL(client.content().contentUrl(hosted, STORE, path));

    final InputStream is = url.openStream();

    byte[] result = IOUtils.toByteArray(is);
    is.close();

    assertThat(result, equalTo(out.toByteArray()));

    // ...and down
    //        FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-out.jar" ), result );

    JarInputStream jarIn = new JarInputStream(new ByteArrayInputStream(result));
    JarEntry jarEntry = jarIn.getNextJarEntry();

    assertThat(jarEntry.getName(), equalTo(entryName));
    String contentResult = IOUtils.toString(jarIn);

    assertThat(contentResult, equalTo(content));
}

From source file:com.izforge.izpack.compiler.container.provider.JarOutputStreamProvider.java

public JarOutputStream provide(CompilerData compilerData) {
    File file = new File(compilerData.getOutput());
    JarOutputStream jarOutputStream = null;
    FileOutputStream fileOutputStream = null;
    FileUtils.deleteQuietly(file);/* w  ww  .j  av  a 2  s. c o m*/
    try {
        if (compilerData.isMkdirs()) {
            FileUtils.forceMkdirParent(file);
        }
        fileOutputStream = new FileOutputStream(file);
        jarOutputStream = new JarOutputStream(fileOutputStream);
        int level = compilerData.getComprLevel();
        if (level >= 0 && level < 10) {
            jarOutputStream.setLevel(level);
        } else {
            jarOutputStream.setLevel(Deflater.BEST_COMPRESSION);
        }
    } catch (IOException e) {
        IOUtils.closeQuietly(fileOutputStream);
    }

    return jarOutputStream;
}

From source file:org.apache.reef.util.JARFileMaker.java

public JARFileMaker(final File outputFile, final Manifest manifest) throws IOException {
    LOG.log(Level.FINER, "Output jar: {0}", outputFile);
    final FileOutputStream outputStream = new FileOutputStream(outputFile);
    this.jarOutputStream = manifest == null ? new JarOutputStream(outputStream)
            : new JarOutputStream(outputStream, manifest);
}

From source file:org.apache.blur.spark.util.JavaSparkUtil.java

private static String createJar(String s) throws IOException {
    File sourceFile = new File(s);
    if (sourceFile.isDirectory()) {
        File file = File.createTempFile(TMP_SPARK_JOB, JAR);
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        JarOutputStream jarOut = new JarOutputStream(outputStream);
        for (File f : sourceFile.listFiles()) {
            pack(sourceFile, f, jarOut);
        }//from  w  w w . j a va2s .c o  m
        jarOut.close();
        file.deleteOnExit();
        return file.getAbsolutePath();
    }
    throw new RuntimeException("File [" + s + "] is not a directory.");
}

From source file:net.adamcin.oakpal.testing.TestPackageUtil.java

public static File prepareTestPackageFromFolder(final String filename, final File srcFolder)
        throws IOException {
    if (srcFolder == null || !srcFolder.isDirectory()) {
        throw new IOException("expected directory in srcFolder parameter for test package filename "
                + String.valueOf(filename));
    }/*from ww  w  .  ja  v  a2  s. co m*/
    File file = new File(testPackagesRoot, filename);
    if (file.exists()) {
        file.delete();
    }

    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(file))) {
        add(srcFolder, srcFolder, jos);
    }

    return file;
}

From source file:com.jaxio.celerio.output.ZipOutputResult.java

@Override
public void open() throws IOException {
    if (isOpen) {
        return;/*from w w w . j a va2s  .  c o m*/
    }
    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(filename)));
    zipOutputStream = new JarOutputStream(bufferedOutputStream);
    isOpen = true;
}

From source file:JarHelper.java

/**
 * Jars a given directory or single file into a JarOutputStream.
 *//*from w ww.  j  ava  2 s.com*/
public void jarDir(File dirOrFile2Jar, File destJar) throws IOException {

    if (dirOrFile2Jar == null || destJar == null)
        throw new IllegalArgumentException();

    mDestJarName = destJar.getCanonicalPath();
    FileOutputStream fout = new FileOutputStream(destJar);
    JarOutputStream jout = new JarOutputStream(fout);
    // jout.setLevel(0);
    try {
        jarDir(dirOrFile2Jar, jout, null);
    } catch (IOException ioe) {
        throw ioe;
    } finally {
        jout.close();
        fout.close();
    }
}