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.eclipse.smarthome.test.SyntheticBundleInstaller.java

private static byte[] createSyntheticBundle(Bundle bundle, String bundlePath, String bundleName,
        Set<String> extensionsToInclude) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Manifest manifest = getManifest(bundle, bundlePath);
    JarOutputStream jarOutputStream = manifest != null ? new JarOutputStream(outputStream, manifest)
            : new JarOutputStream(outputStream);

    List<String> files = collectFilesFrom(bundle, bundlePath, bundleName, extensionsToInclude);
    for (String file : files) {
        addFileToArchive(bundle, bundlePath, file, jarOutputStream);
    }/*from   w w  w . java  2s .  c  o m*/
    jarOutputStream.close();
    return outputStream.toByteArray();
}

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createJar(FileContext files, Path p, LocalResourceVisibility vis) throws IOException {
    LOG.info("Create jar file " + p);
    File jarFile = new File((files.makeQualified(p)).toUri());
    FileOutputStream stream = new FileOutputStream(jarFile);
    LOG.info("Create jar out stream ");
    JarOutputStream out = new JarOutputStream(stream, new Manifest());
    LOG.info("Done writing jar stream ");
    out.close();
    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(p));
    FileStatus status = files.getFileStatus(p);
    ret.setSize(status.getLen());//from w w w .j a  va2s  .c  om
    ret.setTimestamp(status.getModificationTime());
    ret.setType(LocalResourceType.PATTERN);
    ret.setVisibility(vis);
    ret.setPattern("classes/.*");
    return ret;
}

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 {//ww w . ja v  a 2  s.co  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.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);//from ww  w. j  av  a2 s .co m

    File archiveFile = new File(p.toUri().getPath() + ".jar");
    archiveFile.createNewFile();
    JarOutputStream out = new JarOutputStream(new FileOutputStream(archiveFile));
    out.putNextEntry(new JarEntry(p.getName()));
    out.write(bytes);
    out.closeEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".jar")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".jar")).getModificationTime());
    return ret;
}

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

public static void makeZip(File dir, File zipFile) throws IOException, FileNotFoundException {
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(zipFile));
    String as[] = dir.list();/*from ww  w  .jav a  2  s . c  o  m*/
    if (as != null) {
        for (int i = 0; i < as.length; i++)
            recurseFiles(jos, new File(dir, as[i]), "");
    }
    jos.close();
}

From source file:JarUtil.java

/**
 * Writes all given files to the specified jar-file.
 * //www  . j  a v a2 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:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java

/**
 * Add a list of jar files to another jar file under a specific folder.
 * It is used to generated coprocessor jar files which can be loaded by
 * the coprocessor class loader.  It is for testing usage only so we
 * don't be so careful about stream closing in case any exception.
 *
 * @param targetJar the target jar file/* w  w w .ja  v a  2 s.c o  m*/
 * @param libPrefix the folder where to put inner jar files
 * @param srcJars the source inner jar files to be added
 * @throws Exception if anything doesn't work as expected
 */
public static void addJarFilesToJar(File targetJar, String libPrefix, File... srcJars) throws Exception {
    FileOutputStream stream = new FileOutputStream(targetJar);
    JarOutputStream out = new JarOutputStream(stream, new Manifest());
    byte buffer[] = new byte[BUFFER_SIZE];

    for (File jarFile : srcJars) {
        // Add archive entry
        JarEntry jarAdd = new JarEntry(libPrefix + jarFile.getName());
        jarAdd.setTime(jarFile.lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(jarFile);
        while (true) {
            int nRead = in.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            out.write(buffer, 0, nRead);
        }
        in.close();
    }
    out.close();
    stream.close();
    LOG.info("Adding jar file to outer jar file completed");
}

From source file:net.ftb.util.FileUtils.java

/**
 * deletes the META-INF/*from   ww w.j  a  va2 s  . c  o m*/
 */
public static void killMetaInf() {
    File inputFile = new File(Settings.getSettings().getInstallPath() + "/" + ModPack.getSelectedPack().getDir()
            + "/minecraft/bin", "minecraft.jar");
    File outputTmpFile = new File(Settings.getSettings().getInstallPath() + "/"
            + ModPack.getSelectedPack().getDir() + "/minecraft/bin", "minecraft.jar.tmp");
    try {
        JarInputStream input = new JarInputStream(new FileInputStream(inputFile));
        JarOutputStream output = new JarOutputStream(new FileOutputStream(outputTmpFile));
        JarEntry entry;

        while ((entry = input.getNextJarEntry()) != null) {
            if (entry.getName().contains("META-INF")) {
                continue;
            }
            output.putNextEntry(entry);
            byte buffer[] = new byte[1024];
            int amo;
            while ((amo = input.read(buffer, 0, 1024)) != -1) {
                output.write(buffer, 0, amo);
            }
            output.closeEntry();
        }

        input.close();
        output.close();

        if (!inputFile.delete()) {
            Logger.logError("Failed to delete Minecraft.jar.");
            return;
        }
        outputTmpFile.renameTo(inputFile);
    } catch (FileNotFoundException e) {
        Logger.logError("Error while killing META-INF", e);
    } catch (IOException e) {
        Logger.logError("Error while killing META-INF", e);
    }
}

From source file:org.apache.pig.test.TestRegisteredJarVisibility.java

/**
 * Create a jar file containing the generated classes.
 *
 * @param filesToJar map of canonical class name to class file
 * @throws IOException on error/*from w w  w.  jav  a2s .  co  m*/
 */
private static void jar(Map<String, File> filesToJar) throws IOException {
    LOG.info("Creating jar file containing: " + filesToJar);

    JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile.getAbsolutePath()));
    try {
        for (Map.Entry<String, File> entry : filesToJar.entrySet()) {
            String zipEntryName = entry.getKey().replace(".", "/") + ".class";
            LOG.info("Adding " + zipEntryName + " to " + jarFile.getAbsolutePath());
            jos.putNextEntry(new ZipEntry(zipEntryName));
            InputStream classInputStream = new FileInputStream(entry.getValue().getAbsolutePath());
            try {
                ByteStreams.copy(classInputStream, jos);
            } finally {
                classInputStream.close();
            }
        }
    } finally {
        jos.close();
    }
    Assert.assertTrue(jarFile.exists());
    LOG.info("Created " + jarFile.getAbsolutePath());
}

From source file:boa.compiler.BoaCompiler.java

private static void generateJar(final String jarName, final File dir, final List<File> libJars)
        throws IOException, FileNotFoundException {
    final JarOutputStream jar = new JarOutputStream(
            new BufferedOutputStream(new FileOutputStream(new File(jarName))));

    try {/*from   w ww  . ja va2 s. c  om*/
        final int offset = dir.toString().length() + 1;

        for (final File f : findFiles(dir, new ArrayList<File>()))
            putJarEntry(jar, f, f.getPath().substring(offset));

        for (final File f : libJars)
            putJarEntry(jar, f, "lib" + File.separatorChar + f.getName());
    } finally {
        jar.close();
    }
}