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:ai.h2o.servicebuilder.Util.java

/**
 * Create jar archive out of files list. Names in archive have paths starting from relativeToDir
 *
 * @param tobeJared     list of files// w  ww . j av  a2 s  .c  om
 * @param relativeToDir starting directory for paths
 * @return jar as byte array
 * @throws IOException
 */
public static byte[] createJarArchiveByteArray(File[] tobeJared, String relativeToDir) throws IOException {
    int BUFFER_SIZE = 10240;
    byte buffer[] = new byte[BUFFER_SIZE];
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JarOutputStream out = new JarOutputStream(stream, new Manifest());

    for (File t : tobeJared) {
        if (t == null || !t.exists() || t.isDirectory()) {
            if (t != null && !t.isDirectory())
                logger.error("Can't add to jar {}", t);
            continue;
        }
        // Create jar entry
        String filename = t.getPath().replace(relativeToDir, "").replace("\\", "/");
        //      if (filename.endsWith("MANIFEST.MF")) { // skip to avoid duplicates
        //        continue;
        //      }
        JarEntry jarAdd = new JarEntry(filename);
        jarAdd.setTime(t.lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(t);
        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();
    return stream.toByteArray();
}

From source file:org.wso2.carbon.automation.test.utils.common.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//www .  j a  v  a  2  s.  c  o m
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);
            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

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

private Path makeJar(Path p) throws IOException {
    FileOutputStream fos = new FileOutputStream(new File(p.toString()));
    JarOutputStream jos = new JarOutputStream(fos);
    ZipEntry ze = new ZipEntry("test.jar.inside");
    jos.putNextEntry(ze);/* www. j a  va  2  s.  c o m*/
    jos.write(("inside the jar!").getBytes());
    jos.closeEntry();
    jos.close();
    return p;
}

From source file:org.apache.brooklyn.rest.resources.BundleAndTypeResourcesTest.java

private static File createJar(Map<String, String> files) throws Exception {
    File f = Os.newTempFile("osgi", "jar");

    JarOutputStream zip = new JarOutputStream(new FileOutputStream(f));

    for (Map.Entry<String, String> entry : files.entrySet()) {
        JarEntry ze = new JarEntry(entry.getKey());
        zip.putNextEntry(ze);//from www.  j  av a 2 s  .  co m
        zip.write(entry.getValue().getBytes());
    }

    zip.closeEntry();
    zip.flush();
    zip.close();

    return f;
}

From source file:de.cologneintelligence.fitgoodies.maven.FitIntegrationTestMojo.java

public File writeBootJar(String classpath) throws IOException {
    File bootJar = new File(outputDirectory, "boot.jar");

    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, classpath);

    JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(bootJar), manifest);
    jarOutputStream.close();

    return bootJar;
}

From source file:rubah.tools.ConversionClassGenerator.java

public void generateConversionClasses(File outJar) throws IOException {

    FileOutputStream fos = new FileOutputStream(outJar);
    JarOutputStream jos = new JarOutputStream(fos, new Manifest());

    for (Clazz cl : this.namespace.getDefinedClasses()) {
        this.addConversionClassesToJar(jos, cl);
    }//ww w.  java  2  s.c o m

    jos.close();
    fos.close();

}

From source file:org.wso2.esb.integration.common.utils.common.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//  w w  w .j a v  a 2 s.  c  om
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);

            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {

            }
        }
    }

}

From source file:org.apache.hadoop.util.TestApplicationClassLoader.java

private File makeTestJar() throws IOException {
    File jarFile = new File(testDir, "test.jar");
    JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile));
    ZipEntry entry = new ZipEntry("resource.txt");
    out.putNextEntry(entry);//ww  w  .  j  a v  a2s.  c o m
    out.write("hello".getBytes());
    out.closeEntry();
    out.close();
    return jarFile;
}

From source file:JarHelper.java

/**
 * Jars a given directory or single file into a JarOutputStream.
 *//*from ww w.  java  2 s . c o m*/
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();
    }
}

From source file:org.wso2.carbon.integration.common.utils.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//from w w w .jav a2 s .co m
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);
            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }
}