Example usage for org.apache.commons.compress.archivers.jar JarArchiveOutputStream close

List of usage examples for org.apache.commons.compress.archivers.jar JarArchiveOutputStream close

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.jar JarArchiveOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:com.ikon.util.ArchiveUtils.java

/**
 * Recursively create JAR archive from directory 
 */// w w w. j a  v a  2  s.co  m
public static void createJar(File path, String root, OutputStream os) throws IOException {
    log.debug("createJar({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        JarArchiveOutputStream jaos = new JarArchiveOutputStream(os);
        jaos.setComment("Generated by openkm");
        jaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        jaos.setUseLanguageEncodingFlag(true);
        jaos.setFallbackToUTF8(true);
        jaos.setEncoding("UTF-8");

        // Prevents java.util.jar.JarException: JAR file must have at least one entry
        JarArchiveEntry jae = new JarArchiveEntry(root + "/");
        jaos.putArchiveEntry(jae);
        jaos.closeArchiveEntry();

        createJarHelper(path, jaos, root);

        jaos.flush();
        jaos.finish();
        jaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createJar: void");
}

From source file:com.openkm.util.ArchiveUtils.java

/**
 * Recursively create JAR archive from directory
 *//*w w w . jav a  2  s  . c  o  m*/
public static void createJar(File path, String root, OutputStream os) throws IOException {
    log.debug("createJar({}, {}, {})", new Object[] { path, root, os });

    if (path.exists() && path.canRead()) {
        JarArchiveOutputStream jaos = new JarArchiveOutputStream(os);
        jaos.setComment("Generated by OpenKM");
        jaos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);
        jaos.setUseLanguageEncodingFlag(true);
        jaos.setFallbackToUTF8(true);
        jaos.setEncoding("UTF-8");

        // Prevents java.util.jar.JarException: JAR file must have at least one entry
        JarArchiveEntry jae = new JarArchiveEntry(root + "/");
        jaos.putArchiveEntry(jae);
        jaos.closeArchiveEntry();

        createJarHelper(path, jaos, root);

        jaos.flush();
        jaos.finish();
        jaos.close();
    } else {
        throw new IOException("Can't access " + path);
    }

    log.debug("createJar: void");
}

From source file:org.apache.hadoop.hive.ql.processors.CompileProcessor.java

@VisibleForTesting
/**//from w  w w  . j a v a 2 s .c  om
 * Method converts statement into a file, compiles the file and then packages the file.
 * @param ss
 * @return Response code of 0 for success 1 for failure
 * @throws CompileProcessorException
 */
CommandProcessorResponse compile(SessionState ss) throws CompileProcessorException {
    Project proj = new Project();
    String ioTempDir = System.getProperty(IO_TMP_DIR);
    File ioTempFile = new File(ioTempDir);
    if (!ioTempFile.exists()) {
        throw new CompileProcessorException(ioTempDir + " does not exists");
    }
    if (!ioTempFile.isDirectory() || !ioTempFile.canWrite()) {
        throw new CompileProcessorException(ioTempDir + " is not a writable directory");
    }
    Groovyc g = new Groovyc();
    long runStamp = System.currentTimeMillis();
    String jarId = myId + "_" + runStamp;
    g.setProject(proj);
    Path sourcePath = new Path(proj);
    File destination = new File(ioTempFile, jarId + "out");
    g.setDestdir(destination);
    File input = new File(ioTempFile, jarId + "in");
    sourcePath.setLocation(input);
    g.setSrcdir(sourcePath);
    input.mkdir();

    File fileToWrite = new File(input, this.named);
    try {
        Files.write(this.code, fileToWrite, Charset.forName("UTF-8"));
    } catch (IOException e1) {
        throw new CompileProcessorException("writing file", e1);
    }
    destination.mkdir();
    try {
        g.execute();
    } catch (BuildException ex) {
        throw new CompileProcessorException("Problem compiling", ex);
    }
    File testArchive = new File(ioTempFile, jarId + ".jar");
    JarArchiveOutputStream out = null;
    try {
        out = new JarArchiveOutputStream(new FileOutputStream(testArchive));
        for (File f : destination.listFiles()) {
            JarArchiveEntry jentry = new JarArchiveEntry(f.getName());
            FileInputStream fis = new FileInputStream(f);
            out.putArchiveEntry(jentry);
            IOUtils.copy(fis, out);
            fis.close();
            out.closeArchiveEntry();
        }
        out.finish();
    } catch (IOException e) {
        throw new CompileProcessorException("Exception while writing jar", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException WhatCanYouDo) {
            }
        }
    }

    if (ss != null) {
        ss.add_resource(ResourceType.JAR, testArchive.getAbsolutePath());
    }
    CommandProcessorResponse good = new CommandProcessorResponse(0, testArchive.getAbsolutePath(), null);
    return good;
}

From source file:org.moe.cli.executor.AbstractLinkExecutor.java

@Override
public void execute() throws IOException, URISyntaxException, InterruptedException, CheckArchitectureException,
        UnsupportedTypeException {/* w  w  w  .  ja  v a 2s . c o  m*/

    //collect all header files
    List<String> headerList = new ArrayList<String>();
    if (headers != null) {
        for (String header : headers) {
            if (header != null && !header.isEmpty()) {
                File tmp = new File(header);
                if (tmp.isDirectory()) {
                    Collection<File> files = FileUtils.listFiles(tmp, new String[] { "h" }, true);
                    for (File file : files)
                        headerList.add(file.getAbsolutePath());
                } else {
                    headerList.add(header);
                }
            }
        }
    }

    List<String> frameworkList = new ArrayList<String>();
    Set<String> frameworksSearchPaths = new HashSet<String>();
    if (frameworks != null) {

        for (String framework : frameworks) {
            frameworkList.add(framework);
            File frFile = new File(framework);
            frameworksSearchPaths.add(frFile.getParent());
        }

    }

    // Initialize native libraries
    NatJGenNativeLoader.initNatives();

    // Read arguments
    MOEJavaProject project = new MOEJavaProject("", "/");

    boolean generated = true;
    File tmpDir = NatJFileUtils.getNewTempDirectory();
    if (javaSource == null) {

        //generate bindings for all frameworks
        String natJGenBody = NatjGenFrameworkConfig.generate(packageName, frameworksSearchPaths,
                tmpDir.getPath(), headerList);

        //generate bindings
        generated = generate(project, natJGenBody);
    } else {

        Set<URI> links = new HashSet<URI>();
        for (String jsource : javaSource) {
            links.add(new URI(jsource));
        }
        GrabUtils.downloadToFolder(links, tmpDir);
    }

    if (generated) {
        //try to compile generated jars
        File destinationJavacDir = new File(tmpDir, "result");
        destinationJavacDir.mkdir();

        String indePath = System.getenv("MOE_HOME");
        if (indePath != null && !indePath.isEmpty()) {
            String coreJar = indePath + File.separator + "sdk" + File.separator + "moe-core.jar";
            String iosJar = indePath + File.separator + "sdk" + File.separator + "moe-ios.jar";

            String compileList = createCompileFileList(tmpDir, javaSource != null ? null : packageName);

            String classPathArg = String.format("%s:%s", coreJar, iosJar);
            ProcessBuilder processBuilder = new ProcessBuilder("javac", "@" + compileList, "-cp", classPathArg,
                    "-d", destinationJavacDir.getPath());
            Process process = processBuilder.start();
            process.waitFor();

            if (process.exitValue() == 0 || headerList.size() == 0) {
                //try to create lib subdirectory
                File libTemp = new File(destinationJavacDir, "lib");
                if (!(libTemp.mkdir())) {
                    throw new IOException("Could not create temp directory: " + libTemp.getAbsolutePath());
                }

                //try to create bundle subdirectory
                File bundleTemp = new File(destinationJavacDir, "bundle");
                if (!(bundleTemp.mkdir())) {
                    throw new IOException("Could not create temp directory: " + bundleTemp.getAbsolutePath());
                }

                copyLibrary(libTemp, frameworkList);

                if (bundle != null) {
                    copyBundle(bundleTemp, bundle);
                }

                Map<String, String> flags = getManifestProperties(frameworkList);

                //create manifest file
                Manifest manifest = new Manifest();
                manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");

                //Logic from CocoaPods; for more information visit https://github.com/CocoaPods/CocoaPods/issues/3537
                String moe_type = flags.get(MOE_TYPE);
                if (moe_type != null && moe_type.contains("static")) {
                    if (ldFlags != null && !ldFlags.isEmpty()) {
                        if (ldFlags.endsWith(";"))
                            ldFlags += "-ObjC;";
                        else
                            ldFlags += ";-ObjC;";
                    } else {
                        ldFlags = "-ObjC;";
                    }
                }

                if (ldFlags != null && !ldFlags.isEmpty()) {
                    flags.put("MOE_CUSTOM_LINKER_FLAGS", ldFlags);
                }

                for (Map.Entry<String, String> entry : flags.entrySet()) {
                    manifest.getMainAttributes().put(new Attributes.Name(entry.getKey()), entry.getValue());
                }

                //try to create manifest subdirectory
                File manifestTemp = new File(destinationJavacDir, "META-INF");
                if (!(manifestTemp.mkdir())) {
                    throw new IOException("Could not create temp directory: " + bundleTemp.getAbsolutePath());
                }

                String manifestFileName = "MANIFEST.MF";
                File manifestFile = new File(manifestTemp, manifestFileName);
                FileOutputStream manOut = new FileOutputStream(manifestFile);
                manifest.write(manOut);
                manOut.close();

                //try to pack custom content into jar
                File jarFile = new File(outFile);

                FileOutputStream jarFos = null;
                JarArchiveOutputStream target = null;
                try {
                    jarFos = new FileOutputStream(jarFile);
                    target = new JarArchiveOutputStream(jarFos);

                    for (File file : destinationJavacDir.listFiles()) {
                        ArchiveUtils.addFileToJar(destinationJavacDir, file, target);
                    }
                    target.close();

                } finally {
                    if (jarFos != null) {
                        jarFos.close();
                    }

                    if (target != null) {
                        target.close();
                    }
                }
            } else {
                throw new IOException("An error occured during process of bindings compilation");
            }
        } else {
            throw new IOException("Could not find system variable - MOE_HOME");
        }
    } else {
        throw new IOException("An error occured during process of bindings generation");
    }

}