Example usage for java.util.jar JarOutputStream putNextEntry

List of usage examples for java.util.jar JarOutputStream putNextEntry

Introduction

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

Prototype

public void putNextEntry(ZipEntry ze) throws IOException 

Source Link

Document

Begins writing a new JAR file entry and positions the stream to the start of the entry data.

Usage

From source file:boa.compiler.BoaCompiler.java

private static void putJarEntry(final JarOutputStream jar, final File f, final String path) throws IOException {
    jar.putNextEntry(new ZipEntry(path));

    final InputStream in = new BufferedInputStream(new FileInputStream(f));
    try {//  ww w .j  a va 2s .c om
        final byte[] b = new byte[4096];
        int len;
        while ((len = in.read(b)) > 0)
            jar.write(b, 0, len);
    } finally {
        in.close();
    }

    jar.closeEntry();
}

From source file:net.ftb.util.FileUtils.java

/**
 * deletes the META-INF/*from w ww .  ja v a 2 s. c o  m*/
 */
public static void killMetaInf() {
    File inputFile = new File(Settings.getSettings().getInstallPath() + "/" + ModPack.getSelectedPack().getDir()
            + "/minecraft/bin", "minecraft.jar");
    File outputTmpFile = new File(Settings.getSettings().getInstallPath() + "/"
            + ModPack.getSelectedPack().getDir() + "/minecraft/bin", "minecraft.jar.tmp");
    try {
        JarInputStream input = new JarInputStream(new FileInputStream(inputFile));
        JarOutputStream output = new JarOutputStream(new FileOutputStream(outputTmpFile));
        JarEntry entry;

        while ((entry = input.getNextJarEntry()) != null) {
            if (entry.getName().contains("META-INF")) {
                continue;
            }
            output.putNextEntry(entry);
            byte buffer[] = new byte[1024];
            int amo;
            while ((amo = input.read(buffer, 0, 1024)) != -1) {
                output.write(buffer, 0, amo);
            }
            output.closeEntry();
        }

        input.close();
        output.close();

        if (!inputFile.delete()) {
            Logger.logError("Failed to delete Minecraft.jar.");
            return;
        }
        outputTmpFile.renameTo(inputFile);
    } catch (FileNotFoundException e) {
        Logger.logError("Error while killing META-INF", e);
    } catch (IOException e) {
        Logger.logError("Error while killing META-INF", e);
    }
}

From source file:org.eclipse.smarthome.test.SyntheticBundleInstaller.java

private static void addFileToArchive(Bundle bundle, String bundlePath, String fileInBundle,
        JarOutputStream jarOutputStream) throws IOException {
    String filePath = bundlePath + "/" + fileInBundle;
    URL resource = bundle.getResource(filePath);
    if (resource == null) {
        return;//from   ww  w  .ja  v  a 2 s  .  com
    }
    ZipEntry zipEntry = new ZipEntry(fileInBundle);
    jarOutputStream.putNextEntry(zipEntry);
    IOUtils.copy(resource.openStream(), jarOutputStream);
    jarOutputStream.closeEntry();
}

From source file:org.apache.pig.impl.util.JarManager.java

/**
* Adds a stream to a Jar file.//  www. ja  v  a2  s  . c om
*
* @param os
*            the OutputStream of the Jar file to which the stream will be added.
* @param name
*            the name of the stream.
* @param is
*            the stream to add.
* @param contents
*            the current contents of the Jar file. (We use this to avoid adding two streams
*            with the same name.
* @param timestamp
*            timestamp of the entry
* @throws IOException
*/
private static void addStream(JarOutputStream os, String name, InputStream is, Map<String, String> contents,
        long timestamp) throws IOException {
    if (contents.get(name) != null) {
        return;
    }
    contents.put(name, "");
    JarEntry entry = new JarEntry(name);
    entry.setTime(timestamp);
    os.putNextEntry(entry);
    byte buffer[] = new byte[4096];
    int rc;
    while ((rc = is.read(buffer)) > 0) {
        os.write(buffer, 0, rc);
    }
}

From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java

/**
 * Jar a list of files into a jar archive.
 *
 * @param archiveFile the target jar archive
 * @param tobejared a list of files to be jared
 *///from  www . ja v a2 s.  co m
private static boolean createJarArchive(File archiveFile, File[] tobeJared) {
    try {
        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;
            }

            // 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();
        LOG.info("Adding classes to jar file completed");
        return true;
    } catch (Exception ex) {
        LOG.error("Error: " + ex.getMessage());
        return false;
    }
}

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);/*from  ww  w. ja  va2 s  .c  om*/

    File archiveFile = new File(p.toUri().getPath() + ".jar");
    archiveFile.createNewFile();
    JarOutputStream out = new JarOutputStream(new FileOutputStream(archiveFile));
    out.putNextEntry(new JarEntry(p.getName()));
    out.write(bytes);
    out.closeEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".jar")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".jar")).getModificationTime());
    return ret;
}

From source file:JarUtils.java

/**
 * This recursive method writes all matching files and directories to
 * the jar output stream./*from w w  w.  j  av a2s.c  o m*/
 */
private static void jar(File src, String prefix, JarInfo info) throws IOException {

    JarOutputStream jout = info.out;
    if (src.isDirectory()) {
        // create / init the zip entry
        prefix = prefix + src.getName() + "/";
        ZipEntry entry = new ZipEntry(prefix);
        entry.setTime(src.lastModified());
        entry.setMethod(JarOutputStream.STORED);
        entry.setSize(0L);
        entry.setCrc(0L);
        jout.putNextEntry(entry);
        jout.closeEntry();

        // process the sub-directories
        File[] files = src.listFiles(info.filter);
        for (int i = 0; i < files.length; i++) {
            jar(files[i], prefix, info);
        }
    } else if (src.isFile()) {
        // get the required info objects
        byte[] buffer = info.buffer;

        // create / init the zip entry
        ZipEntry entry = new ZipEntry(prefix + src.getName());
        entry.setTime(src.lastModified());
        jout.putNextEntry(entry);

        // dump the file
        FileInputStream in = new FileInputStream(src);
        int len;
        while ((len = in.read(buffer, 0, buffer.length)) != -1) {
            jout.write(buffer, 0, len);
        }
        in.close();
        jout.closeEntry();
    }
}

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

/**
 *
 * @param source//from  w  w  w.j  a  va  2s  .  c  om
 * @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:org.commonjava.web.test.fixture.JarKnockouts.java

public static File rewriteJar(final File source, final File targetDir, final Set<JarKnockouts> jarKnockouts)
        throws IOException {
    final JarKnockouts allKnockouts = new JarKnockouts();
    for (final JarKnockouts jk : jarKnockouts) {
        allKnockouts.knockoutPaths(jk.getKnockedOutPaths());
    }//from w  w w.  j  a va  2s  .c o  m

    targetDir.mkdirs();
    final File target = new File(targetDir, source.getName());

    JarFile in = null;
    JarOutputStream out = null;
    try {
        in = new JarFile(source);

        final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target));
        out = new JarOutputStream(fos, in.getManifest());

        final Enumeration<JarEntry> entries = in.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!allKnockouts.knockout(entry.getName())) {
                final InputStream stream = in.getInputStream(entry);
                out.putNextEntry(entry);
                copy(stream, out);
                out.closeEntry();
            }
        }
    } finally {
        closeQuietly(out);
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
            }
        }
    }

    return target;
}

From source file:gov.nih.nci.restgen.util.GeneratorUtil.java

public static void createJarArchive(File jarFile, File[] listFiles) throws IOException {
    byte b[] = new byte[10240];
    FileOutputStream fout = new FileOutputStream(jarFile);
    JarOutputStream out = new JarOutputStream(fout, new Manifest());
    for (int i = 0; i < listFiles.length; i++) {
        if (listFiles[i] == null || !listFiles[i].exists() || listFiles[i].isDirectory()) {
            System.out.println();
        }//from  ww  w  . ja va2 s.  c  o m
        JarEntry addFiles = new JarEntry(listFiles[i].getName());
        addFiles.setTime(listFiles[i].lastModified());
        out.putNextEntry(addFiles);

        FileInputStream fin = new FileInputStream(listFiles[i]);
        while (true) {
            int len = fin.read(b, 0, b.length);
            if (len <= 0)
                break;
            out.write(b, 0, len);
        }
        fin.close();
    }
    out.close();
    fout.close();
}