Example usage for java.util.jar JarOutputStream write

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

Introduction

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

Prototype

public synchronized void write(byte[] b, int off, int len) throws IOException 

Source Link

Document

Writes an array of bytes to the current ZIP entry data.

Usage

From source file:org.exnebula.bootstrap.TestHelper.java

public static void makeMiniJar(File targetJar, File baseDirectory, String classFile) throws IOException {
    JarOutputStream jar = new JarOutputStream(new FileOutputStream(targetJar));
    jar.putNextEntry(new ZipEntry(classFile));
    byte[] buffer = new byte[4 * 1024];
    InputStream in = new FileInputStream(new File(baseDirectory, classFile));
    int count;/*from  w ww .ja v  a  2  s  .  c o  m*/
    while ((count = in.read(buffer)) > 0) {
        jar.write(buffer, 0, count);
    }
    jar.close();
}

From source file:org.cloudata.core.parallel.hadoop.CloudataMapReduceUtil.java

private static Path makeJarToHDFS(FileSystem fs, Path parentPath, File file) throws IOException {
    Path path = new Path(parentPath, file.getName() + ".jar");

    JarOutputStream out = new JarOutputStream(fs.create(path));
    out.putNextEntry(new JarEntry(file.getName()));

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

    byte[] buf = new byte[1024];

    try {// ww w  .jav a2 s .  co  m
        int readBytes = 0;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
    } finally {
        if (out != null) {
            out.close();
        }

        if (in != null) {
            in.close();
        }
    }

    return path;
}

From source file:JarUtil.java

/**
 * @param entry//from w w  w  .jav  a2  s  .c o  m
 * @param in
 * @param out
 * @param crc
 * @param buffer
 * @throws IOException
 */
private static void add(JarEntry entry, InputStream in, JarOutputStream out, CRC32 crc, byte[] buffer)
        throws IOException {
    out.putNextEntry(entry);
    int read;
    long size = 0;
    while ((read = in.read(buffer)) != -1) {
        crc.update(buffer, 0, read);
        out.write(buffer, 0, read);
        size += read;
    }
    entry.setCrc(crc.getValue());
    entry.setSize(size);
    in.close();
    out.closeEntry();
    crc.reset();
}

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

/**
* Adds a stream to a Jar file./*from   w w  w . 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:net.ftb.util.FileUtils.java

/**
 * deletes the META-INF/*from w  ww.ja  va  2 s .co  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.gemini.blueprint.test.internal.util.jar.JarUtils.java

/**
 * /*from  w w  w. j  a va  2s.c o  m*/
 * Writes a resource content to a jar.
 * 
 * @param res
 * @param entryName
 * @param jarStream
 * @param bufferSize
 * @return the number of bytes written to the jar file
 * @throws Exception
 */
public static int writeToJar(Resource res, String entryName, JarOutputStream jarStream, int bufferSize)
        throws IOException {
    byte[] readWriteJarBuffer = new byte[bufferSize];

    // remove leading / if present.
    if (entryName.charAt(0) == '/')
        entryName = entryName.substring(1);

    jarStream.putNextEntry(new ZipEntry(entryName));
    InputStream entryStream = res.getInputStream();

    int numberOfBytes;

    // read data into the buffer which is later on written to the jar.
    while ((numberOfBytes = entryStream.read(readWriteJarBuffer)) != -1) {
        jarStream.write(readWriteJarBuffer, 0, numberOfBytes);
    }
    return numberOfBytes;
}

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

/**
 * Add a list of jar files to another jar file under a specific folder.
 * It is used to generated coprocessor jar files which can be loaded by
 * the coprocessor class loader.  It is for testing usage only so we
 * don't be so careful about stream closing in case any exception.
 *
 * @param targetJar the target jar file/*from  w w w.  j  av  a2s.  co m*/
 * @param libPrefix the folder where to put inner jar files
 * @param srcJars the source inner jar files to be added
 * @throws Exception if anything doesn't work as expected
 */
public static void addJarFilesToJar(File targetJar, String libPrefix, File... srcJars) throws Exception {
    FileOutputStream stream = new FileOutputStream(targetJar);
    JarOutputStream out = new JarOutputStream(stream, new Manifest());
    byte buffer[] = new byte[BUFFER_SIZE];

    for (File jarFile : srcJars) {
        // Add archive entry
        JarEntry jarAdd = new JarEntry(libPrefix + jarFile.getName());
        jarAdd.setTime(jarFile.lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(jarFile);
        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 jar file to outer jar file completed");
}

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 {//from   ww w.  j  a v a2  s  .  com
        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: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  w  ww .  jav  a 2  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();
}

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
 *///ww  w.ja v a  2  s .  c om
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;
    }
}