Example usage for java.util.jar JarOutputStream JarOutputStream

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

Introduction

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

Prototype

public JarOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new JarOutputStream with no manifest.

Usage

From source file:org.apache.crunch.WordCountHBaseTest.java

@SuppressWarnings("deprecation")
@Before//  w w  w  .  j  ava2s  .com
public void setUp() throws Exception {
    Configuration conf = hbaseTestUtil.getConfiguration();
    File tmpDir = File.createTempFile("logdir", "");
    tmpDir.delete();
    tmpDir.mkdir();
    tmpDir.deleteOnExit();
    conf.set("hadoop.log.dir", tmpDir.getAbsolutePath());
    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
    conf.setInt("hbase.master.info.port", -1);
    conf.setInt("hbase.regionserver.info.port", -1);

    hbaseTestUtil.startMiniZKCluster();
    hbaseTestUtil.startMiniCluster();
    hbaseTestUtil.startMiniMapReduceCluster(1);

    // For Hadoop-2.0.0, we have to do a bit more work.
    if (TaskAttemptContext.class.isInterface()) {
        conf = hbaseTestUtil.getConfiguration();
        FileSystem fs = FileSystem.get(conf);
        Path tmpPath = new Path("target", "WordCountHBaseTest-tmpDir");
        FileSystem localFS = FileSystem.getLocal(conf);
        for (FileStatus jarFile : localFS.listStatus(new Path("target/lib/"))) {
            Path target = new Path(tmpPath, jarFile.getPath().getName());
            fs.copyFromLocalFile(jarFile.getPath(), target);
            DistributedCache.addFileToClassPath(target, conf, fs);
        }

        // Create a programmatic container for this jar.
        JarOutputStream jos = new JarOutputStream(new FileOutputStream("WordCountHBaseTest.jar"));
        File baseDir = new File("target/test-classes");
        String prefix = "org/apache/crunch/";
        jarUp(jos, baseDir, prefix + "WordCountHBaseTest.class");
        jarUp(jos, baseDir, prefix + "WordCountHBaseTest$1.class");
        jarUp(jos, baseDir, prefix + "WordCountHBaseTest$2.class");
        jos.close();

        Path target = new Path(tmpPath, "WordCountHBaseTest.jar");
        fs.copyFromLocalFile(true, new Path("WordCountHBaseTest.jar"), target);
        DistributedCache.addFileToClassPath(target, conf, fs);
    }
}

From source file:org.wso2.esb.integration.common.utils.common.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//from   w w w .  j  a v  a 2s . c om
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            InputStream inputStream = jarFile.getInputStream(jarEntry);

            //jarOutputStream.putNextEntry(jarEntry);
            //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
            jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {

            }
        }
    }

}

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

public void createJar(OutputStream outputStream) {
    try {//from www  .j a v a 2 s  . c  om
        JarOutputStream jarOutputStream = new JarOutputStream(outputStream);
        createJar(jarOutputStream);
        jarOutputStream.finish();
    } catch (IOException e) {
        LOGGER.error("", e);
    }
}

From source file:com.h3xstream.findbugs.test.service.FindBugsLauncher.java

/**
 * The minimum requirement to have a "valid" archive plugin is to include
 * findbugs.xml, messages.xml and MANIFEST.MF files. The rest of the
 * resources are load using the parent ClassLoader (Not requires to be in
 * the jar)./*from www . j av  a2s.co  m*/
 * <p>
 * Instead of building a file on disk, the result of the stream is kept in
 * memory and return as a byte array.
 *
 * @return
 * @throws IOException
 * @throws URISyntaxException 
 */
private byte[] buildFakePluginJar() throws IOException, URISyntaxException {
    ClassLoader cl = getClass().getClassLoader();

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    JarOutputStream jar = new JarOutputStream(buffer);

    final URL metadata = cl.getResource("metadata");
    if (metadata != null) {
        final File dir = new File(metadata.toURI());

        //Add files to the jar stream
        addFilesToStream(cl, jar, dir, "");
    }
    jar.finish();
    jar.close();

    return buffer.toByteArray();
}

From source file:org.sonatype.maven.plugin.emma4it.InstrumentProjectArtifactMojo.java

private void appendEmma(File instrumentedFile) throws MojoExecutionException, IOException {
    File original = new File(instrumentedFile.getParentFile(), "original.jar");
    FileUtils.copyFile(instrumentedFile, original);
    instrumentedFile.delete();/*from   w w w .  ja va 2  s  . c  o  m*/

    Artifact emma = artifactFactory.createArtifact("emma", "emma", "2.0.5312", "compile", "jar");
    try {
        resolver.resolve(emma, remoteRepositories, localRepository);
    } catch (AbstractArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }

    JarOutputStream finalJar = new JarOutputStream(new FileOutputStream(instrumentedFile));
    try {
        append(finalJar, original, false);
        append(finalJar, emma.getFile(), true);
    } finally {
        IOUtil.close(finalJar);
    }
}

From source file:org.wso2.carbon.automation.test.utils.common.FileManager.java

public void copyJarFile(String sourceFileLocationWithFileName, String destinationDirectory)
        throws IOException, URISyntaxException {
    File sourceFile = new File(getClass().getResource(sourceFileLocationWithFileName).toURI());
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);
    JarOutputStream jarOutputStream = null;
    try {//from  w  ww. j  a  v  a 2 s  .co m
        jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
        Enumeration<JarEntry> entries = jarFile.entries();
        InputStream inputStream = null;
        while (entries.hasMoreElements()) {
            try {
                JarEntry jarEntry = entries.nextElement();
                inputStream = jarFile.getInputStream(jarEntry);
                //jarOutputStream.putNextEntry(jarEntry);
                //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
                jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    jarOutputStream.write(buffer, 0, bytesRead);
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                    jarOutputStream.flush();
                    jarOutputStream.closeEntry();
                }
            }
        }
    } finally {
        if (jarOutputStream != null) {
            jarOutputStream.close();
        }
    }
}

From source file:org.roda.core.util.ZipUtility.java

/**
 * Creates ZIP file with the files inside directory <code>contentsDir</code> .
 * //from   w  ww  . ja v  a 2 s .c  o  m
 * @param newZipFile
 *          the ZIP file to create
 * @param contentsDir
 *          the directory containing the files to compress.
 * @return the created ZIP file.
 * @throws IOException
 *           if something goes wrong with creation of the ZIP file or the
 *           reading of the files to compress.
 */
public static File createZIPFile(File newZipFile, File contentsDir) throws IOException {

    List<File> contentAbsoluteFiles = FileUtility.listFilesRecursively(contentsDir);

    FileOutputStream zipStream = new FileOutputStream(newZipFile);
    JarOutputStream jarOutputStream = new JarOutputStream(new BufferedOutputStream(zipStream));

    // Create a buffer for reading the files
    byte[] buffer = new byte[BUFFER_SIZE];

    Iterator<File> iterator = contentAbsoluteFiles.iterator();
    while (iterator.hasNext()) {
        File absoluteFile = iterator.next();
        String relativeFile = getFilePathRelativeTo(absoluteFile, contentsDir);

        FileInputStream inputStream = new FileInputStream(absoluteFile);
        BufferedInputStream in = new BufferedInputStream(inputStream);

        // Add ZIP entry to output stream.
        jarOutputStream.putNextEntry(new JarEntry(relativeFile));

        LOGGER.trace("Adding {}", relativeFile);

        int length;
        while ((length = in.read(buffer)) > 0) {
            jarOutputStream.write(buffer, 0, length);
        }

        // Complete the entry
        jarOutputStream.closeEntry();
        in.close();
        inputStream.close();
    }

    // Complete the ZIP file
    jarOutputStream.close();
    zipStream.close();

    return newZipFile;
}

From source file:org.apache.hadoop.util.TestApplicationClassLoader.java

private File makeTestJar() throws IOException {
    File jarFile = new File(testDir, "test.jar");
    JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile));
    ZipEntry entry = new ZipEntry("resource.txt");
    out.putNextEntry(entry);/*from  w w w  . j a va2  s .com*/
    out.write("hello".getBytes());
    out.closeEntry();
    out.close();
    return jarFile;
}

From source file:org.apache.maven.classpath.munger.validation.JarValidationUtilsTest.java

private static byte[] createTestJar(Collection<? extends ZipEntry> entriesList, byte... data)
        throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(
            data.length + entriesList.size() * 64 * 2 + Byte.MAX_VALUE);
    try (JarOutputStream jarFile = new JarOutputStream(baos)) {
        for (ZipEntry zipEntry : entriesList) {
            jarFile.putNextEntry(zipEntry);
            try {
                if (zipEntry.isDirectory()) {
                    continue;
                }//from ww w  .  ja v a  2s.  c om
                jarFile.write(data);
            } finally {
                jarFile.closeEntry();
            }
        }
    } finally {
        baos.close();
    }

    return baos.toByteArray();
}

From source file:net.technicpack.launchercore.util.ZipUtils.java

public static void copyMinecraftJar(File minecraft, File output) throws IOException {
    String[] security = { "MOJANG_C.DSA", "MOJANG_C.SF", "CODESIGN.RSA", "CODESIGN.SF" };
    JarFile jarFile = new JarFile(minecraft);
    try {/*from   www. ja v a2  s  .  com*/
        String fileName = jarFile.getName();
        String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));

        JarOutputStream jos = new JarOutputStream(new FileOutputStream(output));
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (containsAny(entry.getName(), security)) {
                continue;
            }
            InputStream is = jarFile.getInputStream(entry);

            //jos.putNextEntry(entry);
            //create a new entry to avoid ZipException: invalid entry compressed size
            jos.putNextEntry(new JarEntry(entry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                jos.write(buffer, 0, bytesRead);
            }
            is.close();
            jos.flush();
            jos.closeEntry();
        }
        jos.close();
    } finally {
        jarFile.close();
    }

}