Example usage for java.util.jar JarOutputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP output stream as well as the stream being filtered.

Usage

From source file:org.apache.hadoop.filecache.TestMRWithDistributedCache.java

private Path makeJar(Path p, int index) throws FileNotFoundException, IOException {
    FileOutputStream fos = new FileOutputStream(new File(p.toString()));
    JarOutputStream jos = new JarOutputStream(fos);
    ZipEntry ze = new ZipEntry("distributed.jar.inside" + index);
    jos.putNextEntry(ze);//from   w w w  .j  av  a 2  s  . c o  m
    jos.write(("inside the jar!" + index).getBytes());
    jos.closeEntry();
    jos.close();
    return p;
}

From source file:org.roda.core.util.ZipUtility.java

/**
 * Creates ZIP file with the files inside directory <code>contentsDir</code> .
 * /*from w  ww.j  a  v a2s.  co  m*/
 * @param newZipFile
 *          the ZIP file to create
 * @param contentsDir
 *          the directory containing the files to compress.
 * @return the created ZIP file.
 * @throws IOException
 *           if something goes wrong with creation of the ZIP file or the
 *           reading of the files to compress.
 */
public static File createZIPFile(File newZipFile, File contentsDir) throws IOException {

    List<File> contentAbsoluteFiles = FileUtility.listFilesRecursively(contentsDir);

    FileOutputStream zipStream = new FileOutputStream(newZipFile);
    JarOutputStream jarOutputStream = new JarOutputStream(new BufferedOutputStream(zipStream));

    // Create a buffer for reading the files
    byte[] buffer = new byte[BUFFER_SIZE];

    Iterator<File> iterator = contentAbsoluteFiles.iterator();
    while (iterator.hasNext()) {
        File absoluteFile = iterator.next();
        String relativeFile = getFilePathRelativeTo(absoluteFile, contentsDir);

        FileInputStream inputStream = new FileInputStream(absoluteFile);
        BufferedInputStream in = new BufferedInputStream(inputStream);

        // Add ZIP entry to output stream.
        jarOutputStream.putNextEntry(new JarEntry(relativeFile));

        LOGGER.trace("Adding {}", relativeFile);

        int length;
        while ((length = in.read(buffer)) > 0) {
            jarOutputStream.write(buffer, 0, length);
        }

        // Complete the entry
        jarOutputStream.closeEntry();
        in.close();
        inputStream.close();
    }

    // Complete the ZIP file
    jarOutputStream.close();
    zipStream.close();

    return newZipFile;
}

From source file:com.streamsets.datacollector.cluster.TestTarFileCreator.java

private File createJar(File parentDir) throws IOException {
    if (parentDir.isFile()) {
        parentDir.delete();// w  w  w.ja va2 s.c om
    }
    parentDir.mkdirs();
    Assert.assertTrue(parentDir.isDirectory());
    String uuid = UUID.randomUUID().toString();
    File jar = new File(parentDir, uuid + ".jar");
    JarOutputStream out = new JarOutputStream(new FileOutputStream(jar));
    JarEntry entry = new JarEntry("sample.txt");
    out.putNextEntry(entry);
    out.write(uuid.getBytes(StandardCharsets.UTF_8));
    out.closeEntry();
    out.close();
    return jar;
}

From source file:ezbake.frack.submitter.util.JarUtil.java

public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException {
    JarOutputStream target = null;
    JarInputStream source;/*from   w w w .j  a v  a 2  s .co  m*/
    File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath()));
    log.debug("Output file created at {}", outputJar.getAbsolutePath());
    outputJar.createNewFile();
    try {
        source = new JarInputStream(new FileInputStream(sourceJar));
        target = new JarOutputStream(new FileOutputStream(outputJar));
        ZipEntry entry = source.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            // Add ZIP entry to output stream.
            target.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((len = source.read(buffer)) > 0) {
                target.write(buffer, 0, len);
            }
            entry = source.getNextEntry();
        }
        source.close();
        for (File fileToAdd : newFiles) {
            add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target);
        }
    } finally {
        if (target != null) {
            log.debug("Closing output stream");
            target.close();
        }
    }
    return outputJar;
}

From source file:com.facebook.buck.java.JarDirectoryStepTest.java

private Manifest jarDirectoryAndReadManifest(Manifest fromJar, Manifest fromUser, boolean mergeEntries)
        throws IOException {
    // Create a jar with a manifest we'd expect to see merged.
    Path originalJar = folder.newFile("unexpected.jar");
    JarOutputStream ignored = new JarOutputStream(Files.newOutputStream(originalJar), fromJar);
    ignored.close();

    // Now create the actual manifest
    Path manifestFile = folder.newFile("actual_manfiest.mf");
    try (OutputStream os = Files.newOutputStream(manifestFile)) {
        fromUser.write(os);// w  ww . j ava 2s.c o  m
    }

    Path tmp = folder.newFolder();
    Path output = tmp.resolve("example.jar");
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(tmp), output,
            ImmutableSortedSet.of(originalJar), /* main class */ null, manifestFile, mergeEntries,
            /* blacklist */ ImmutableSet.<String>of());
    ExecutionContext context = TestExecutionContext.newInstance();
    step.execute(context);

    // Now verify that the created manifest matches the expected one.
    try (JarInputStream jis = new JarInputStream(Files.newInputStream(output))) {
        return jis.getManifest();
    }
}

From source file:org.apache.apex.malhar.sql.schema.TupleSchemaRegistry.java

public String generateCommonJar() throws IOException {
    File file = File.createTempFile("schemaSQL", ".jar");

    FileSystem fs = FileSystem.newInstance(file.toURI(), new Configuration());
    FSDataOutputStream out = fs.create(new Path(file.getAbsolutePath()));
    JarOutputStream jout = new JarOutputStream(out);

    for (Schema schema : schemas.values()) {
        jout.putNextEntry(new ZipEntry(schema.fqcn.replace(".", "/") + ".class"));
        jout.write(schema.beanClassBytes);
        jout.closeEntry();//from w  w w .  j a va  2s  . co m
    }

    jout.close();
    out.close();

    return file.getAbsolutePath();
}

From source file:edu.stanford.muse.email.JarDocCache.java

private synchronized void packHeaders(String prefix) throws IOException {
    headersMap.remove(prefix);//from   w w w  .  j  a  va2 s  . c  o m
    JarOutputStream jos = headersJarOSMap.remove(prefix);
    if (jos != null)
        jos.close();
}

From source file:edu.stanford.muse.email.JarDocCache.java

private synchronized void packContents(String prefix) throws IOException {
    contentsMap.remove(prefix);//from www.  java  2s  .  c  o m
    JarOutputStream jos = contentsJarOSMap.remove(prefix);
    if (jos != null)
        jos.close();
}

From source file:com.h3xstream.findbugs.test.service.FindBugsLauncher.java

/**
 * 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)./* ww w.j a  va2s. c  om*/
 * <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();
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

private static void replaceInJar(File sourceJar, String origFile, File replacementFile) throws IOException {
    String srcJarAbsPath = sourceJar.getAbsolutePath();
    String srcJarSuffix = srcJarAbsPath.substring(srcJarAbsPath.lastIndexOf(File.separator) + 1);
    String srcJarName = srcJarSuffix.split(".jar")[0];

    String destJarName = srcJarName + "-managix";
    String destJarSuffix = destJarName + ".jar";
    File destJar = new File(sourceJar.getParentFile().getAbsolutePath() + File.separator + destJarSuffix);
    //  File destJar = new File(sourceJar.getAbsolutePath() + ".modified");
    JarFile sourceJarFile = new JarFile(sourceJar);
    Enumeration<JarEntry> entries = sourceJarFile.entries();
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(destJar));
    byte[] buffer = new byte[2048];
    int read;// ww w. java  2 s.com
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = entry.getName();
        if (name.equals(origFile)) {
            continue;
        }
        InputStream jarIs = sourceJarFile.getInputStream(entry);
        jos.putNextEntry(entry);
        while ((read = jarIs.read(buffer)) != -1) {
            jos.write(buffer, 0, read);
        }
        jarIs.close();
    }
    sourceJarFile.close();
    JarEntry entry = new JarEntry(origFile);
    jos.putNextEntry(entry);
    FileInputStream fis = new FileInputStream(replacementFile);
    while ((read = fis.read(buffer)) != -1) {
        jos.write(buffer, 0, read);
    }
    fis.close();
    jos.close();
    sourceJar.delete();
    destJar.renameTo(sourceJar);
    destJar.setExecutable(true);
}