Example usage for java.util.jar JarEntry JarEntry

List of usage examples for java.util.jar JarEntry JarEntry

Introduction

In this page you can find the example usage for java.util.jar JarEntry JarEntry.

Prototype

public JarEntry(JarEntry je) 

Source Link

Document

Creates a new JarEntry with fields taken from the specified JarEntry object.

Usage

From source file:Main.java

void createJarArchive(File archiveFile, File[] tobeJared) throws Exception {
    byte buffer[] = new byte[BUFFER_SIZE];
    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...
        JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
        jarAdd.setTime(tobeJared[i].lastModified());
        out.putNextEntry(jarAdd);/*  www . j  av  a  2 s . c o m*/
        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();
}

From source file:CreateJarFile.java

protected void createJarArchive(File archiveFile, File[] tobeJared) {
    try {/* w  w  w.j a va  2 s  . 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:JarUtils.java

/**
 * Add a jar entry to the deployment archive
 *///w  w  w .  j ava 2s  .c  om
public static void addJarEntry(JarOutputStream outputStream, String entryName, InputStream inputStream)
        throws IOException {
    outputStream.putNextEntry(new JarEntry(entryName));
    copyStream(outputStream, inputStream);
}

From source file:com.scorpio4.util.io.JarArchiver.java

public String add(InputStream inputStream, String filename, String comment, long size)
        throws IOException, NoSuchAlgorithmException {
    if (jarOutputStream == null)
        throw new IOException("not open");
    JarEntry entry = new JarEntry(filename);
    entry.setSize(size);//from   www.j  a va2s  .c  o  m

    entry.setMethod(JarEntry.DEFLATED); // compressed
    entry.setTime(System.currentTimeMillis());
    if (comment != null)
        entry.setComment(comment);
    jarOutputStream.putNextEntry(entry);

    // copy I/O & return SHA-1 fingerprint
    String fingerprint = Fingerprint.copy(inputStream, jarOutputStream, 4096);
    IOUtils.copy(inputStream, jarOutputStream);
    log.debug("JAR add " + size + " bytes: " + filename + " -> " + fingerprint);

    jarOutputStream.closeEntry();
    //      jarOutputStream.finish();
    return fingerprint;
}

From source file:com.enderville.enderinstaller.util.InstallScript.java

/**
 * Repackages all the files in the tmp directory to the new minecraft.jar
 *
 * @param tmp The temp directory where mods were installed.
 * @param mcjar The location to save the new minecraft.jar.
 * @throws IOException//from   w ww . j av a  2  s .  c o  m
 */
public static void repackMCJar(File tmp, File mcjar) throws IOException {
    byte[] dat = new byte[4 * 1024];

    JarOutputStream jarout = new JarOutputStream(FileUtils.openOutputStream(mcjar));

    Queue<File> queue = new LinkedList<File>();
    for (File f : tmp.listFiles()) {
        queue.add(f);
    }

    while (!queue.isEmpty()) {
        File f = queue.poll();
        if (f.isDirectory()) {
            for (File child : f.listFiles()) {
                queue.add(child);
            }
        } else {
            //TODO need a better way to do this
            String name = f.getPath().substring(tmp.getPath().length() + 1);
            //TODO is this formatting really required for jars?
            name = name.replace("\\", "/");
            if (f.isDirectory() && !name.endsWith("/")) {
                name = name + "/";
            }
            JarEntry entry = new JarEntry(name);
            jarout.putNextEntry(entry);

            FileInputStream in = new FileInputStream(f);
            int len = -1;
            while ((len = in.read(dat)) > 0) {
                jarout.write(dat, 0, len);
            }
            in.close();
        }
        jarout.closeEntry();
    }
    jarout.close();

}

From source file:oz.hadoop.yarn.api.utils.JarUtils.java

/**
 *
 * @param source/*w  ww .j a  va  2 s  .co  m*/
 * @param lengthOfOriginalPath
 * @param target
 * @throws IOException
 */
private static void add(File source, int lengthOfOriginalPath, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    try {
        String path = source.getAbsolutePath();
        path = path.substring(lengthOfOriginalPath);

        if (source.isDirectory()) {
            String name = path.replace("\\", "/");
            if (!name.isEmpty()) {
                if (!name.endsWith("/")) {
                    name += "/";
                }
                JarEntry entry = new JarEntry(name.substring(1)); // avoiding absolute path warning
                target.putNextEntry(entry);
                target.closeEntry();
            }

            for (File nestedFile : source.listFiles()) {
                add(nestedFile, lengthOfOriginalPath, target);
            }

            return;
        }

        JarEntry entry = new JarEntry(path.replace("\\", "/").substring(1)); // avoiding absolute path warning
        entry.setTime(source.lastModified());
        try {
            target.putNextEntry(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.closeEntry();
        } catch (Exception e) {
            String message = e.getMessage();
            if (StringUtils.hasText(message)) {
                if (!message.toLowerCase().contains("duplicate")) {
                    throw new IllegalStateException(e);
                }
                logger.warn(message);
            } else {
                throw new IllegalStateException(e);
            }
        }
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:com.dragome.callbackevictor.serverside.utils.RewritingUtils.java

public static boolean rewriteJar(final JarInputStream pInput, final ResourceTransformer transformer,
        final JarOutputStream pOutput, final Matcher pMatcher) throws IOException {

    boolean changed = false;

    while (true) {
        final JarEntry entry = pInput.getNextJarEntry();

        if (entry == null) {
            break;
        }/*ww w  .ja  v a  2  s . com*/

        if (entry.isDirectory()) {
            pOutput.putNextEntry(new JarEntry(entry));
            continue;
        }

        final String name = entry.getName();

        pOutput.putNextEntry(new JarEntry(name));

        if (name.endsWith(".class")) {
            if (pMatcher.isMatching(name)) {

                if (log.isDebugEnabled()) {
                    log.debug("transforming " + name);
                }

                final byte[] original = toByteArray(pInput);

                byte[] transformed = transformer.transform(original);

                pOutput.write(transformed);

                changed |= transformed.length != original.length;

                continue;
            }
        } else if (name.endsWith(".jar") || name.endsWith(".ear") || name.endsWith(".zip")
                || name.endsWith(".war")) {

            changed |= rewriteJar(new JarInputStream(pInput), transformer, new JarOutputStream(pOutput),
                    pMatcher);

            continue;
        }

        int length = copy(pInput, pOutput);

        log.debug("copied " + name + "(" + length + ")");
    }

    pInput.close();
    pOutput.close();

    return changed;
}

From source file:com.speed.ob.api.ClassStore.java

public void init(JarInputStream jarIn, File output, File in) throws IOException {
    ZipEntry entry;//from  w  w  w .j a va 2  s. c o  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:JarUtils.java

/**
 * Create a jar file from a particular directory.
 * /*from  w  w w  . j a v a  2  s  . co  m*/
 * @param root in the root directory
 * @param directory in the directory we are adding
 * @param jarStream the jar stream to be added to
 * @throws IOException on IOException
 */
protected void createJarFromDirectory(File root, File directory, JarOutputStream jarStream) throws IOException {
    byte[] buffer = new byte[40960];
    int bytesRead;

    File[] filesToAdd = directory.listFiles();

    for (int i = 0; i < filesToAdd.length; i++) {
        File fileToAdd = filesToAdd[i];

        if (fileToAdd.isDirectory()) {
            createJarFromDirectory(root, fileToAdd, jarStream);
        } else {
            FileInputStream addFile = new FileInputStream(fileToAdd);
            try {
                // Create a jar entry and add it to the temp jar.
                String entryName = fileToAdd.getPath().substring(root.getPath().length() + 1);

                // If we leave these entries as '\'s, then the resulting zip file won't be
                // expandable on Unix operating systems like OSX, because it is possible to 
                // have filenames with \s in them - so it's impossible to determine that this 
                // is actually a directory.
                entryName = entryName.replace('\\', '/');
                JarEntry entry = new JarEntry(entryName);
                jarStream.putNextEntry(entry);

                // Read the file and write it to the jar.
                while ((bytesRead = addFile.read(buffer)) != -1) {
                    jarStream.write(buffer, 0, bytesRead);
                }
                jarStream.closeEntry();
            } finally {
                addFile.close();
            }
        }
    }
}

From source file:ezbake.frack.submitter.util.JarUtil.java

private static void add(File source, final String prefix, JarOutputStream target) throws IOException {
    BufferedInputStream in = null;
    log.debug("Adding file {} to jar", source.getName());
    try {/*from  w  w w  .ja v  a  2s.  c  o m*/
        String entryPath = source.getPath().replace("\\", "/").replace(prefix, "");
        if (entryPath.startsWith("/")) {
            entryPath = entryPath.substring(1);
        }
        if (source.isDirectory()) {
            if (!entryPath.isEmpty()) {
                if (!entryPath.endsWith("/")) {
                    entryPath += "/";
                }
                JarEntry entry = new JarEntry(entryPath);
                entry.setTime(source.lastModified());
                target.putNextEntry(entry);
                target.closeEntry();
            }
            for (File nestedFile : source.listFiles()) {
                add(nestedFile, prefix, target);
            }
        } else {
            JarEntry entry = new JarEntry(entryPath);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            in = new BufferedInputStream(new FileInputStream(source));

            byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = in.read(buffer)) > 0) {
                target.write(buffer, 0, len);
            }
            target.closeEntry();
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}