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

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

Introduction

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

Prototype

public void closeArchiveEntry() throws IOException 

Source Link

Document

Writes all necessary data for this entry.

Usage

From source file:com.offbynull.coroutines.instrumenter.testhelpers.TestUtils.java

private static void writeJarEntry(JarArchiveOutputStream jaos, String name, byte[] data) throws IOException {
    ZipArchiveEntry entry = new JarArchiveEntry(name);
    entry.setSize(data.length);/*  w ww .j  a va 2 s.  co m*/
    jaos.putArchiveEntry(entry);
    jaos.write(data);
    jaos.closeArchiveEntry();
}

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

/**
 * Recursively create JAR archive from directory helper utility 
 *//*  w  ww .  j av a  2s .  c om*/
private static void createJarHelper(File fs, JarArchiveOutputStream jaos, String zePath) throws IOException {
    log.debug("createJarHelper({}, {}, {})", new Object[] { fs, jaos, zePath });
    File[] files = fs.listFiles();

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            log.debug("DIRECTORY {}", files[i]);
            JarArchiveEntry jae = new JarArchiveEntry(zePath + "/" + files[i].getName() + "/");
            jaos.putArchiveEntry(jae);
            jaos.closeArchiveEntry();

            createJarHelper(files[i], jaos, zePath + "/" + files[i].getName());
        } else {
            log.debug("FILE {}", files[i]);
            JarArchiveEntry jae = new JarArchiveEntry(zePath + "/" + files[i].getName());
            jaos.putArchiveEntry(jae);
            FileInputStream fis = new FileInputStream(files[i]);
            IOUtils.copy(fis, jaos);
            fis.close();
            jaos.closeArchiveEntry();
        }
    }

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

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

/**
 * Recursively create JAR archive from directory 
 *//*  w ww .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
 *//*from  w ww . j a  va2s .  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:com.jrummyapps.busybox.signing.ZipSigner.java

/** Copy all the files in a manifest from input to output. */
private static void copyFiles(Manifest manifest, JarFile in, JarArchiveOutputStream out, long timestamp)
        throws IOException {
    final byte[] buffer = new byte[4096];
    int num;/*from   ww w .  ja va2 s .co m*/

    final Map<String, Attributes> entries = manifest.getEntries();
    final List<String> names = new ArrayList<>(entries.keySet());
    Collections.sort(names);
    for (final String name : names) {
        final JarEntry inEntry = in.getJarEntry(name);
        if (inEntry.getMethod() == JarArchiveEntry.STORED) {
            // Preserve the STORED method of the input entry.
            out.putArchiveEntry(new JarArchiveEntry(inEntry));
        } else {
            // Create a new entry so that the compressed len is recomputed.
            final JarArchiveEntry je = new JarArchiveEntry(name);
            je.setTime(timestamp);
            out.putArchiveEntry(je);
        }

        final InputStream data = in.getInputStream(inEntry);
        while ((num = data.read(buffer)) > 0) {
            out.write(buffer, 0, num);
        }
        out.flush();
        out.closeArchiveEntry();
    }
}

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

@VisibleForTesting
/**//from w  ww  .  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.apache.openejb.maven.plugin.customizer.monkey.jar.JarPatcher.java

private void jar(final int method, final JarArchiveOutputStream jar, final File f, final String prefix)
        throws IOException {
    final String path = f.getPath().replace(prefix, "").replace(File.separator, "/");
    final ZipArchiveEntry zip = new ZipArchiveEntry(f, path);
    zip.setMethod(method);/*w  w w  .  j  ava 2  s.  c o m*/
    final JarArchiveEntry archiveEntry = new JarArchiveEntry(zip);
    jar.putArchiveEntry(archiveEntry);
    if (f.isDirectory()) {
        jar.closeArchiveEntry();
        final File[] files = f.listFiles();
        if (files != null) {
            for (final File child : files) {
                jar(method, jar, child.getCanonicalFile(), prefix);
            }
        }
    } else {
        final InputStream is = new FileInputStream(f);
        IOUtils.copy(is, jar);
        is.close();
        jar.closeArchiveEntry();
    }
}

From source file:org.apache.openejb.maven.plugin.customizer.monkey.MonkeyTest.java

private File prepareProject() throws IOException {
    final File target = new File("target/MonkeyTest_run" + System.currentTimeMillis() + "/mvn/target");
    target.mkdirs();/*  ww w. ja v  a  2s.com*/

    final File classes = new File(target, "classes");
    classes.mkdirs();

    writeBinary(classes, "target/test-classes/test/patch/MyMain.class", "test/patch/MyMain.class");
    writeBinary(classes, "target/test-classes/test/patch/foo/Another.class", "test/patch/foo/Another.class");

    final File tomee = new File(target, "tomee");
    final File lib = new File(tomee, "lib");
    lib.mkdirs();

    // create the jar to patch, it is invalid but when patched it should work
    JarArchiveOutputStream stream = null;
    try {
        stream = new JarArchiveOutputStream(new FileOutputStream(new File(lib, "t.jar")));
        stream.putArchiveEntry(new JarArchiveEntry("test/patch/MyMain.class"));
        stream.write("invalid".getBytes());
        stream.closeArchiveEntry();
        stream.putArchiveEntry(new JarArchiveEntry("test/patch/foo/Another.class"));
        stream.write("invalid-too".getBytes());
        stream.closeArchiveEntry();
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }

    return tomee;
}

From source file:org.moe.cli.utils.ArchiveUtils.java

public static void addFileToJar(File baseDir, File source, JarArchiveOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {//  w  w w  .  java2 s. c o  m
        String baseName = baseDir.getPath().replace("\\", "/");
        baseName = baseName.endsWith("/") ? baseName : baseName + "/";
        String name = source.getPath().replace("\\", "/").replace(baseName, "");
        if (source.isDirectory()) {

            if (!name.isEmpty()) {
                if (!name.endsWith("/"))
                    name += "/";
                JarArchiveEntry entry = new JarArchiveEntry(name);
                entry.setTime(source.lastModified());
                target.putArchiveEntry(entry);
                target.closeArchiveEntry();
            }
            for (File nestedFile : source.listFiles())
                addFileToJar(baseDir, nestedFile, target);
            return;
        }

        JarArchiveEntry entry = new JarArchiveEntry(name);
        entry.setTime(source.lastModified());
        target.putArchiveEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[1024];
        while (true) {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeArchiveEntry();
    } finally {
        if (in != null)
            in.close();
    }
}