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:org.atricore.josso.tooling.wrapper.InstallCommand.java

private void createJar(File outFile, String resource) throws Exception {
    if (!outFile.exists()) {
        System.out.println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD)
                .a(outFile.getPath()).a(Ansi.Attribute.RESET).toString());
        InputStream is = getClass().getClassLoader().getResourceAsStream(resource);
        if (is == null) {
            throw new IllegalStateException("Resource " + resource + " not found!");
        }/*from  w  w  w  . ja v a  2  s . c o m*/
        try {
            JarOutputStream jar = new JarOutputStream(new FileOutputStream(outFile));
            int idx = resource.indexOf('/');
            while (idx > 0) {
                jar.putNextEntry(new ZipEntry(resource.substring(0, idx)));
                jar.closeEntry();
                idx = resource.indexOf('/', idx + 1);
            }
            jar.putNextEntry(new ZipEntry(resource));
            int c;
            while ((c = is.read()) >= 0) {
                jar.write(c);
            }
            jar.closeEntry();
            jar.close();
        } finally {
            safeClose(is);
        }
    }
}

From source file:com.googlecode.mycontainer.maven.plugin.ExecMojo.java

/**
 * Create a jar with just a manifest containing a Main-Class entry for
 * SurefireBooter and a Class-Path entry for all classpath elements. Copied
 * from surefire (ForkConfiguration#createJar())
 * //from  w  ww .  j ava  2 s  .  co m
 * @param classPath
 *            List<String> of all classpath elements.
 * @return
 * @throws IOException
 */
private File createJar(List classPath, String mainClass) throws IOException {
    File file = File.createTempFile("maven-exec", ".jar");
    file.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(file);
    JarOutputStream jos = new JarOutputStream(fos);
    jos.setLevel(JarOutputStream.STORED);
    JarEntry je = new JarEntry("META-INF/MANIFEST.MF");
    jos.putNextEntry(je);

    Manifest man = new Manifest();

    // we can't use StringUtils.join here since we need to add a '/' to
    // the end of directory entries - otherwise the jvm will ignore them.
    String cp = "";
    for (Iterator it = classPath.iterator(); it.hasNext();) {
        String el = (String) it.next();
        // NOTE: if File points to a directory, this entry MUST end in '/'.
        cp += UrlUtils.getURL(new File(el)).toExternalForm() + " ";
    }

    man.getMainAttributes().putValue("Manifest-Version", "1.0");
    man.getMainAttributes().putValue("Class-Path", cp.trim());
    man.getMainAttributes().putValue("Main-Class", mainClass);

    man.write(jos);
    jos.close();

    return file;
}

From source file:org.bimserver.plugins.VirtualFile.java

private void createJar(JarOutputStream jarOutputStream) throws IOException {
    for (VirtualFile virtualFile : files.values()) {
        if (virtualFile.isDirectory()) {
            virtualFile.createJar(jarOutputStream);
        } else {//from w w w . j av  a  2s .  c  om
            JarEntry jarEntry = new JarEntry(virtualFile.getName().replace(File.separator, "/"));
            jarOutputStream.putNextEntry(jarEntry);
            InputStream inputStream = virtualFile.openInputStream();
            IOUtils.copy(inputStream, jarOutputStream);
            inputStream.close();
        }
    }
}

From source file:edu.stanford.muse.email.JarDocCache.java

@Override
public synchronized void saveHeader(Document d, String prefix, int msgNum)
        throws FileNotFoundException, IOException {
    JarOutputStream jos = getHeadersJarOS(prefix);

    // create the bytes
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream headerOOS = new ObjectOutputStream(baos);
    headerOOS.writeObject(d);//from ww w  .  ja  va 2s. co  m
    byte buf[] = baos.toByteArray();

    ZipEntry ze = new ZipEntry(msgNum + ".header");
    ze.setMethod(ZipEntry.DEFLATED);
    jos.putNextEntry(ze);
    jos.write(buf, 0, buf.length);
    jos.closeEntry();
    jos.flush();
}

From source file:au.com.addstar.objects.Plugin.java

/**
 * Adds the Spigot.ver file to the jar//from   ww  w  .j  av  a  2 s.c o  m
 *
 * @param ver
 */
public void addSpigotVer(String ver) {
    if (latestFile == null)
        return;
    File newFile = new File(latestFile.getParentFile(),
            SpigotUpdater.getFormat().format(Calendar.getInstance().getTime()) + "-" + ver + "-s.jar");
    File spigotFile = new File(latestFile.getParentFile(), "spigot.ver");
    if (spigotFile.exists())
        FileUtils.deleteQuietly(spigotFile);
    try {
        JarFile oldjar = new JarFile(latestFile);
        if (oldjar.getEntry("spigot.ver") != null)
            return;
        JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(newFile));
        try (Writer wr = new FileWriter(spigotFile); BufferedWriter writer = new BufferedWriter(wr)) {
            writer.write(ver);
            writer.newLine();
        }
        try (FileInputStream stream = new FileInputStream(spigotFile)) {
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            JarEntry je = new JarEntry(spigotFile.getName());
            tempJarOutputStream.putNextEntry(je);
            while ((bytesRead = stream.read(buffer)) != -1) {
                tempJarOutputStream.write(buffer, 0, bytesRead);
            }
            stream.close();
        }
        Enumeration jarEntries = oldjar.entries();
        while (jarEntries.hasMoreElements()) {
            JarEntry entry = (JarEntry) jarEntries.nextElement();
            InputStream entryInputStream = oldjar.getInputStream(entry);
            tempJarOutputStream.putNextEntry(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = entryInputStream.read(buffer)) != -1) {
                tempJarOutputStream.write(buffer, 0, bytesRead);
            }
            entryInputStream.close();
        }
        tempJarOutputStream.close();
        oldjar.close();
        FileUtils.deleteQuietly(latestFile);
        FileUtils.deleteQuietly(spigotFile);
        FileUtils.moveFile(newFile, latestFile);
        latestFile = newFile;

    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.kepler.kar.KARBuilder.java

/**
 *
 */// ww w  .  ja v  a  2 s.  c  o  m
private void writeKARFile() throws IOException {

    JarOutputStream jos = new JarOutputStream(new FileOutputStream(_karFile), _manifest);
    Iterator<KAREntry> li = _karItems.keySet().iterator();
    while (li.hasNext()) {
        KAREntry entry = (KAREntry) li.next();
        if (isDebugging)
            log.debug("Writing " + entry.getName());
        try {
            jos.putNextEntry(entry);

            if (_karItems.get(entry) instanceof InputStream) {
                // inputstream from a bin file
                byte[] b = new byte[1024];
                InputStream is = (InputStream) _karItems.get(entry);
                int numread = is.read(b, 0, 1024);
                while (numread != -1) {
                    jos.write(b, 0, numread);
                    numread = is.read(b, 0, 1024);
                }
                is.close();
                // jos.flush();
                jos.closeEntry();
            }
        } catch (IOException ioe) {
            log.error(" Tried to write Duplicate Entry to kar " + entry.getName() + " " + entry.getLSID());
            ioe.printStackTrace();
        }
    }
    jos.flush();
    jos.close();

    log.info("done writing KAR file to " + _karFile.getAbsolutePath());
}

From source file:com.android.builder.testing.MockableJarGenerator.java

public void createMockableJar(File input, File output) throws IOException {
    Preconditions.checkState(output.createNewFile(), "Output file [%s] already exists.",
            output.getAbsolutePath());/*from  w  w  w.j a  v a 2s  .  c o m*/

    JarFile androidJar = null;
    JarOutputStream outputStream = null;
    try {
        androidJar = new JarFile(input);
        outputStream = new JarOutputStream(new FileOutputStream(output));

        for (JarEntry entry : Collections.list(androidJar.entries())) {
            InputStream inputStream = androidJar.getInputStream(entry);

            if (entry.getName().endsWith(".class")) {
                if (!skipClass(entry.getName().replace("/", "."))) {
                    rewriteClass(entry, inputStream, outputStream);
                }
            } else {
                outputStream.putNextEntry(entry);
                ByteStreams.copy(inputStream, outputStream);
            }

            inputStream.close();
        }
    } finally {
        if (androidJar != null) {
            androidJar.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:org.dspace.installer_edm.InstallerCrosswalk.java

/**
 * Aade contenido archivo class a nuevo archivo class en el jar
 *
 * @param jarOutputStream flujo de escritura para el jar
 * @throws IOException//www . jav  a 2s .  co m
 */
protected void addClass2Jar(JarOutputStream jarOutputStream) throws IOException {
    File file = new File(myInstallerWorkDirPath + fileSeparator + edmCrossWalkClass);
    byte[] fileData = new byte[(int) file.length()];
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    dis.readFully(fileData);
    dis.close();
    jarOutputStream.putNextEntry(new JarEntry(edmCrossWalkClass));
    jarOutputStream.write(fileData);
    jarOutputStream.closeEntry();
}

From source file:org.apache.phoenix.end2end.UserDefinedFunctionsIT.java

/**
 * Compiles the test class with bogus code into a .class file.
 * Upon finish, the bogus jar will be left at dynamic.jar.dir location
 *//*from   w  w w .j av a  2s . com*/
private static void compileTestClass(String className, String program, int counter) throws Exception {
    String javaFileName = className + ".java";
    File javaFile = new File(javaFileName);
    String classFileName = className + ".class";
    File classFile = new File(classFileName);
    String jarName = "myjar" + counter + ".jar";
    String jarPath = "." + File.separator + jarName;
    File jarFile = new File(jarPath);
    try {
        String packageName = "org.apache.phoenix.end2end";
        FileOutputStream fos = new FileOutputStream(javaFileName);
        fos.write(program.getBytes());
        fos.close();

        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
        int result = jc.run(null, null, null, javaFileName);
        assertEquals(0, result);

        Manifest manifest = new Manifest();
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
        FileOutputStream jarFos = new FileOutputStream(jarPath);
        JarOutputStream jarOutputStream = new JarOutputStream(jarFos, manifest);
        String pathToAdd = packageName.replace('.', '/') + '/';
        String jarPathStr = new String(pathToAdd);
        Set<String> pathsInJar = new HashSet<String>();

        while (pathsInJar.add(jarPathStr)) {
            int ix = jarPathStr.lastIndexOf('/', jarPathStr.length() - 2);
            if (ix < 0) {
                break;
            }
            jarPathStr = jarPathStr.substring(0, ix);
        }
        for (String pathInJar : pathsInJar) {
            jarOutputStream.putNextEntry(new JarEntry(pathInJar));
            jarOutputStream.closeEntry();
        }

        jarOutputStream.putNextEntry(new JarEntry(pathToAdd + classFile.getName()));
        byte[] allBytes = new byte[(int) classFile.length()];
        FileInputStream fis = new FileInputStream(classFile);
        fis.read(allBytes);
        fis.close();
        jarOutputStream.write(allBytes);
        jarOutputStream.closeEntry();
        jarOutputStream.close();
        jarFos.close();

        assertTrue(jarFile.exists());
        Connection conn = driver.connect(url, EMPTY_PROPS);
        Statement stmt = conn.createStatement();
        stmt.execute("add jars '" + jarFile.getAbsolutePath() + "'");
    } finally {
        if (javaFile != null)
            javaFile.delete();
        if (classFile != null)
            classFile.delete();
        if (jarFile != null)
            jarFile.delete();
    }
}

From source file:org.eclipse.birt.build.mavenrepogen.RepoGen.java

private void createJar(final File jarFile, final File[] files) throws IOException {
    final Manifest manifest = new Manifest();
    final Attributes attributes = manifest.getMainAttributes();
    attributes.putValue("Manifest-Version", "1.0");
    attributes.putValue("Created-By", "RepoGen 1.0.0");
    final FileOutputStream fos = new FileOutputStream(jarFile);
    final JarOutputStream jos = new JarOutputStream(fos, manifest);
    for (final File file : files) {
        final ZipEntry entry = new ZipEntry(file.getName());
        jos.putNextEntry(entry);
        final FileInputStream fis = new FileInputStream(file);
        pipeStream(fis, jos);// w  w w.j  a v  a  2 s. com
        fis.close();
    }
    jos.close();
}