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:com.speed.ob.api.ClassStore.java

public void init(JarInputStream jarIn, File output, File in) throws IOException {
    ZipEntry entry;/*w  ww . j  a v a  2 s .  co  m*/
    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:CreateJarFile.java

protected void createJarArchive(File archiveFile, File[] tobeJared) {
    try {//from  w  w w .jav a 2s .  co m
        byte buffer[] = new byte[BUFFER_SIZE];
        // Open archive file
        FileOutputStream stream = new FileOutputStream(archiveFile);
        JarOutputStream out = new JarOutputStream(stream, new Manifest());

        for (int i = 0; i < tobeJared.length; i++) {
            if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory())
                continue; // Just in case...
            System.out.println("Adding " + tobeJared[i].getName());

            // Add archive entry
            JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
            jarAdd.setTime(tobeJared[i].lastModified());
            out.putNextEntry(jarAdd);

            // Write file to archive
            FileInputStream in = new FileInputStream(tobeJared[i]);
            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();
        System.out.println("Adding completed OK");
    } catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("Error: " + ex.getMessage());
    }
}

From source file:org.eclipse.birt.build.mavenrepogen.RepoGen.java

private void createJar(final File jarFile, final File[] files) throws IOException {
    final Manifest manifest = new Manifest();
    final Attributes attributes = manifest.getMainAttributes();
    attributes.putValue("Manifest-Version", "1.0");
    attributes.putValue("Created-By", "RepoGen 1.0.0");
    final FileOutputStream fos = new FileOutputStream(jarFile);
    final JarOutputStream jos = new JarOutputStream(fos, manifest);
    for (final File file : files) {
        final ZipEntry entry = new ZipEntry(file.getName());
        jos.putNextEntry(entry);/*from  w  ww.ja  va 2 s  .  co  m*/
        final FileInputStream fis = new FileInputStream(file);
        pipeStream(fis, jos);
        fis.close();
    }
    jos.close();
}

From source file:org.eclipse.equinox.servletbridge.FrameworkLauncher.java

/**
 * deployExtensionBundle will generate the Servletbridge extensionbundle if it is not already present in the platform's
 * plugin directory. By default it exports "org.eclipse.equinox.servletbridge" and a versioned export of the Servlet API.
 * Additional exports can be added by using the "extendedFrameworkExports" initial-param in the ServletConfig
 *//*www .ja va2 s.c om*/
private void deployExtensionBundle(File plugins) {
    File extensionBundle = new File(plugins, "org.eclipse.equinox.servletbridge.extensionbundle_1.0.0.jar"); //$NON-NLS-1$
    File extensionBundleDir = new File(plugins, "org.eclipse.equinox.servletbridge.extensionbundle_1.0.0"); //$NON-NLS-1$
    if (Boolean.valueOf(config.getInitParameter(CONFIG_OVERRIDE_AND_REPLACE_EXTENSION_BUNDLE)).booleanValue()) {
        extensionBundle.delete();
        deleteDirectory(extensionBundleDir);
    } else if (extensionBundle.exists() || extensionBundleDir.isDirectory())
        return;

    Manifest mf = new Manifest();
    Attributes attribs = mf.getMainAttributes();
    attribs.putValue(MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
    attribs.putValue(BUNDLE_MANIFEST_VERSION, "2"); //$NON-NLS-1$
    attribs.putValue(BUNDLE_NAME, "Servletbridge Extension Bundle"); //$NON-NLS-1$
    attribs.putValue(BUNDLE_SYMBOLIC_NAME, "org.eclipse.equinox.servletbridge.extensionbundle"); //$NON-NLS-1$
    attribs.putValue(BUNDLE_VERSION, "1.0.0"); //$NON-NLS-1$
    attribs.putValue(FRAGMENT_HOST, "system.bundle; extension:=framework"); //$NON-NLS-1$

    String servletVersion = context.getMajorVersion() + "." + context.getMinorVersion(); //$NON-NLS-1$
    String packageExports = "org.eclipse.equinox.servletbridge; version=1.0" + //$NON-NLS-1$
            ", javax.servlet; version=" + servletVersion + //$NON-NLS-1$
            ", javax.servlet.http; version=" + servletVersion + //$NON-NLS-1$
            ", javax.servlet.resources; version=" + servletVersion; //$NON-NLS-1$

    String extendedExports = config.getInitParameter(CONFIG_EXTENDED_FRAMEWORK_EXPORTS);
    if (extendedExports != null && extendedExports.trim().length() != 0)
        packageExports += ", " + extendedExports; //$NON-NLS-1$

    attribs.putValue(EXPORT_PACKAGE, packageExports);

    try {
        JarOutputStream jos = null;
        try {
            jos = new JarOutputStream(new FileOutputStream(extensionBundle), mf);
            jos.finish();
        } finally {
            if (jos != null)
                jos.close();
        }
    } catch (IOException e) {
        context.log("Error generating extension bundle", e); //$NON-NLS-1$
    }
}

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

/**
 * creates a jar containing a UDF not in the current classloader
 * @param jarFile the jar to create//w w w .  ja v  a  2 s.co  m
 * @return the name of the class created (in the default package)
 * @throws IOException
 * @throws FileNotFoundException
 */
private String createTestJar(File jarFile) throws IOException, FileNotFoundException {

    // creating the source .java file
    File javaFile = File.createTempFile("TestUDF", ".java");
    javaFile.deleteOnExit();
    String className = javaFile.getName().substring(0, javaFile.getName().lastIndexOf('.'));
    FileWriter fw = new FileWriter(javaFile);
    try {
        fw.write("import org.apache.pig.EvalFunc;\n");
        fw.write("import org.apache.pig.data.Tuple;\n");
        fw.write("import java.io.IOException;\n");
        fw.write("public class " + className + " extends EvalFunc<String> {\n");
        fw.write("  public String exec(Tuple input) throws IOException {\n");
        fw.write("    return \"test\";\n");
        fw.write("  }\n");
        fw.write("}\n");
    } finally {
        fw.close();
    }

    // compiling it
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjects(javaFile);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits1);
    task.call();

    // here is the compiled file
    File classFile = new File(javaFile.getParentFile(), className + ".class");
    Assert.assertTrue(classFile.exists());

    // putting it in the jar
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
    try {
        jos.putNextEntry(new ZipEntry(classFile.getName()));
        try {
            InputStream testClassContentIS = new FileInputStream(classFile);
            try {
                byte[] buffer = new byte[64000];
                int n;
                while ((n = testClassContentIS.read(buffer)) != -1) {
                    jos.write(buffer, 0, n);
                }
            } finally {
                testClassContentIS.close();
            }
        } finally {
            jos.closeEntry();
        }
    } finally {
        jos.close();
    }

    return className;
}

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

public void copyJarFile(String sourceFileLocationWithFileName, String destinationDirectory)
        throws IOException, URISyntaxException {
    File sourceFile = new File(getClass().getResource(sourceFileLocationWithFileName).toURI());
    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 = 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);
        }/*w w  w .j  ava 2 s . c o m*/
        inputStream.close();
        jarOutputStream.flush();
        jarOutputStream.closeEntry();
    }
    jarOutputStream.close();
}

From source file:com.wolvereness.overmapped.OverMapped.java

private void writeToFile(final MultiProcessor executor, final Map<String, ByteClass> byteClasses,
        final List<Pair<ZipEntry, byte[]>> fileEntries, final BiMap<String, String> nameMaps,
        final BiMap<Signature, Signature> signatureMaps, final Map<Signature, Integer> flags)
        throws IOException, FileNotFoundException, InterruptedException, ExecutionException {
    final Collection<Future<Pair<ZipEntry, byte[]>>> classWrites = newArrayList();
    for (final ByteClass clazz : byteClasses.values()) {
        classWrites.add(//from   w w  w. j  av a2  s.  c o  m
                executor.submit(clazz.callable(signatureMaps, nameMaps, byteClasses, flags, correctEnums)));
    }

    FileOutputStream fileOut = null;
    JarOutputStream jar = null;
    try {
        jar = new JarOutputStream(fileOut = new FileOutputStream(output));
        for (final Pair<ZipEntry, byte[]> fileEntry : fileEntries) {
            jar.putNextEntry(fileEntry.getLeft());
            jar.write(fileEntry.getRight());
        }
        for (final Future<Pair<ZipEntry, byte[]>> fileEntryFuture : classWrites) {
            final Pair<ZipEntry, byte[]> fileEntry = fileEntryFuture.get();
            jar.putNextEntry(fileEntry.getLeft());
            jar.write(fileEntry.getRight());
        }
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (final IOException ex) {
            }
        }
        if (fileOut != null) {
            try {
                fileOut.close();
            } catch (final IOException ex) {
            }
        }
    }
}

From source file:com.android.builder.testing.MockableJarGenerator.java

public void createMockableJar(File input, File output) throws IOException {
    Preconditions.checkState(output.createNewFile(), "Output file [%s] already exists.",
            output.getAbsolutePath());/*from  ww  w  .  ja v  a  2s .com*/

    JarFile androidJar = null;
    JarOutputStream outputStream = null;
    try {
        androidJar = new JarFile(input);
        outputStream = new JarOutputStream(new FileOutputStream(output));

        for (JarEntry entry : Collections.list(androidJar.entries())) {
            InputStream inputStream = androidJar.getInputStream(entry);

            if (entry.getName().endsWith(".class")) {
                if (!skipClass(entry.getName().replace("/", "."))) {
                    rewriteClass(entry, inputStream, outputStream);
                }
            } else {
                outputStream.putNextEntry(entry);
                ByteStreams.copy(inputStream, outputStream);
            }

            inputStream.close();
        }
    } finally {
        if (androidJar != null) {
            androidJar.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:org.atricore.josso.tooling.wrapper.InstallCommand.java

private void createJar(File outFile, String resource) throws Exception {
    if (!outFile.exists()) {
        System.out.println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD)
                .a(outFile.getPath()).a(Ansi.Attribute.RESET).toString());
        InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
        if (is == null) {
            throw new IllegalStateException("Resource " + resource + " not found!");
        }//from   w ww .ja  v a  2 s  . com
        try {
            JarOutputStream jar = new JarOutputStream(new FileOutputStream(outFile));
            int idx = resource.indexOf('/');
            while (idx > 0) {
                jar.putNextEntry(new ZipEntry(resource.substring(0, idx)));
                jar.closeEntry();
                idx = resource.indexOf('/', idx + 1);
            }
            jar.putNextEntry(new ZipEntry(resource));
            int c;
            while ((c = is.read()) >= 0) {
                jar.write(c);
            }
            jar.closeEntry();
            jar.close();
        } finally {
            safeClose(is);
        }
    }
}

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

/**
 * @param jobConf//from   www. j  a v a2 s.c o m
 * @throws IOException
 * @throws FileNotFoundException
 */
private void uploadJobJar(JobConf jobConf) throws IOException, FileNotFoundException {
    File jobJarFile = new File(TEST_ROOT_DIR, "jobjar-on-dfs.jar");
    JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jobJarFile));
    ZipEntry ze = new ZipEntry("lib/lib1.jar");
    jstream.putNextEntry(ze);
    jstream.closeEntry();
    ze = new ZipEntry("lib/lib2.jar");
    jstream.putNextEntry(ze);
    jstream.closeEntry();
    jstream.finish();
    jstream.close();
    jobConf.setJar(jobJarFile.toURI().toString());
}