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:com.digitalreasoning.herman.JarCreater.java

public static void createJar(File outputJarFile, List<Entry> entries) throws IOException {
    if (!outputJarFile.getParentFile().exists()) {
        outputJarFile.getParentFile().mkdirs();
    }/*from ww w  .  ja  va  2  s.c o m*/
    if (!outputJarFile.exists()) {
        outputJarFile.createNewFile();
    }

    FileOutputStream fOut = new FileOutputStream(outputJarFile);
    JarOutputStream jarOut = new JarOutputStream(fOut);
    Set<String> packageSet = new HashSet<String>();
    try {
        for (Entry folderFile : entries) {
            InputStream inputStream = folderFile.resource.openStream();

            try {
                if (!packageSet.contains(folderFile.parentFolderName)) {
                    jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName));
                    jarOut.closeEntry();
                    packageSet.add(folderFile.parentFolderName);
                }

                jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName
                        + (folderFile.parentFolderName.endsWith("/") ? "" : "/") + folderFile.fileName));
                IOUtils.copy(inputStream, jarOut);
                jarOut.closeEntry();
            } finally {
                inputStream.close();
            }

        }

    } finally {
        jarOut.close();
        fOut.close();
    }
}

From source file:org.sourcepit.osgifier.core.packaging.BundleLocalizationWriter.java

public static Map<String, LocalizedData> write(final JarOutputStream out, Manifest manifest,
        BundleLocalization localization) throws IOException {
    final BundleLocalizationWriter<JarOutputStream> writer = new BundleLocalizationWriter<JarOutputStream>() {
        @Override/*from w w  w  .j a v  a  2 s.  co m*/
        protected JarOutputStream openStream(String path) throws IOException {
            out.putNextEntry(new JarEntry(path));
            return out;
        }

        @Override
        protected void closeStream(JarOutputStream out, String path) throws IOException {
            out.closeEntry();
        }
    };
    return writer.write(manifest, localization);
}

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

        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:org.eclipse.gemini.blueprint.test.internal.util.jar.JarUtils.java

/**
 * /* w  w w.ja  va 2  s  . co  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.eclipse.wb.tests.designer.TestUtils.java

/**
 * @return the path to the temporary "jar" file with single entry.
 * //from ww w  .  j a v  a2s .co m
 * @param entryName
 *          the name of entry, for example <code>"myFolder/subFolder/file.txt"</code>.
 * @param content
 *          the {@link String} content of entry.
 */
public static String createTemporaryJar(String entryName, String content) throws Exception {
    File tempFile = File.createTempFile("wbpTests", ".jar");
    tempFile.deleteOnExit();
    // create "jar" with single entry
    {
        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tempFile));
        jarOutputStream.putNextEntry(new ZipEntry(entryName));
        jarOutputStream.write(content.getBytes());
        jarOutputStream.closeEntry();
        jarOutputStream.close();
    }
    // return path to "jar"
    return tempFile.getAbsolutePath();
}

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// w  w  w.  ja  v a  2  s  .  c o 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:org.apache.sling.tooling.support.install.impl.InstallServlet.java

private static void createJar(final File sourceDir, final File jarFile, final Manifest mf) throws IOException {
    final JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
    try {//from  w w w  .  ja  va2  s  .  c om
        zos.setLevel(Deflater.NO_COMPRESSION);
        // manifest first
        final ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
        zos.putNextEntry(anEntry);
        mf.write(zos);
        zos.closeEntry();
        zipDir(sourceDir, zos, "");
    } finally {
        try {
            zos.close();
        } catch (final IOException ignore) {
            // ignore
        }
    }
}

From source file:com.germinus.easyconf.FileUtil.java

public static void writeAsJAR(File dest, String propsFileName, Properties props)
        throws FileNotFoundException, IOException {
    JarOutputStream out = new JarOutputStream(new FileOutputStream(dest));
    JarEntry propertiesFile = new JarEntry(propsFileName);
    propertiesFile.setExtra(propertiesToString(props).getBytes());
    out.putNextEntry(propertiesFile);
    out.close();/*from   w  w w  .  java  2s .  c  o  m*/
}

From source file:JarMaker.java

/**
 * used by downloadAndPack//  ww  w.j  av  a2  s.co m
 * @param _jout
 * @param _dir
 * @param _prefix
 * @throws IOException
 */
public static void add(JarOutputStream _jout, File _dir, String _prefix) throws IOException {

    File[] content = _dir.listFiles();
    if (_dir.isDirectory()) {
        for (int i = 0, l = content.length; i < l; ++i) {
            if (content[i].isDirectory()) {
                _jout.putNextEntry(
                        new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + content[i].getName() + "/"));
                add(_jout, content[i], _prefix + (_prefix.equals("") ? "" : "/") + content[i].getName());
            } else {
                _jout.putNextEntry(
                        new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + content[i].getName()));
                FileInputStream in = new FileInputStream(content[i]);
                write(in, _jout);
                in.close();
            }
        }
    } else {
        _jout.putNextEntry(new ZipEntry(_prefix + (_prefix.equals("") ? "" : "/") + _dir.getName()));
        FileInputStream in = new FileInputStream(_dir);
        write(in, _jout);
        in.close();
    }
}

From source file:org.apache.pig.test.TestRegisteredJarVisibility.java

/**
 * Create a jar file containing the generated classes.
 *
 * @param filesToJar map of canonical class name to class file
 * @throws IOException on error//from  w w  w  .  ja  v a 2s. c  om
 */
private static void jar(Map<String, File> filesToJar) throws IOException {
    LOG.info("Creating jar file containing: " + filesToJar);

    JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile.getAbsolutePath()));
    try {
        for (Map.Entry<String, File> entry : filesToJar.entrySet()) {
            String zipEntryName = entry.getKey().replace(".", "/") + ".class";
            LOG.info("Adding " + zipEntryName + " to " + jarFile.getAbsolutePath());
            jos.putNextEntry(new ZipEntry(zipEntryName));
            InputStream classInputStream = new FileInputStream(entry.getValue().getAbsolutePath());
            try {
                ByteStreams.copy(classInputStream, jos);
            } finally {
                classInputStream.close();
            }
        }
    } finally {
        jos.close();
    }
    Assert.assertTrue(jarFile.exists());
    LOG.info("Created " + jarFile.getAbsolutePath());
}