Example usage for java.util.jar JarInputStream getNextJarEntry

List of usage examples for java.util.jar JarInputStream getNextJarEntry

Introduction

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

Prototype

public JarEntry getNextJarEntry() throws IOException 

Source Link

Document

Reads the next JAR file entry and positions the stream at the beginning of the entry data.

Usage

From source file:org.jahia.utils.maven.plugin.support.MavenAetherHelperUtils.java

public static Set<PackageInfo> getJarPackages(File jarFile, boolean optionalJar, String version,
        ParsingContext parsingContext, Log log) {
    JarInputStream jarInputStream = null;
    Set<PackageInfo> packageInfos = new LinkedHashSet<PackageInfo>();
    if (jarFile == null) {
        log.warn("File is null !");
        return packageInfos;
    }/* w ww . j  a v a  2 s. c o m*/
    if (!jarFile.exists()) {
        log.warn("File " + jarFile + " does not exist !");
        return packageInfos;
    }
    log.debug("Scanning JAR " + jarFile + "...");
    try {
        jarInputStream = new JarInputStream(new FileInputStream(jarFile));
        JarEntry jarEntry = null;
        while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
            String jarPackageName = jarEntry.getName().replaceAll("/", ".");
            if (jarPackageName.endsWith(".")) {
                jarPackageName = jarPackageName.substring(0, jarPackageName.length() - 1);
            }
            if (jarPackageName.startsWith("META-INF") || jarPackageName.startsWith("WEB-INF")
                    || jarPackageName.startsWith("OSGI-INF")) {
                continue;
            }
            packageInfos.addAll(PackageUtils.getPackagesFromClass(jarPackageName, optionalJar, version,
                    jarFile.getCanonicalPath(), parsingContext));
        }
    } catch (IOException e) {
        log.error(e);
        ;
    } finally {
        IOUtils.closeQuietly(jarInputStream);
    }
    return packageInfos;
}

From source file:io.joynr.util.JoynrUtil.java

public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException {

    JarFile jf = null;//from  w w  w .  jav  a 2 s  . co  m
    JarInputStream jarInputStream = null;

    try {
        jf = new JarFile(jarName);
        JarEntry je = jf.getJarEntry(srcDir);
        if (je.isDirectory()) {
            FileInputStream fis = new FileInputStream(jarName);
            BufferedInputStream bis = new BufferedInputStream(fis);
            jarInputStream = new JarInputStream(bis);
            JarEntry ze = null;
            while ((ze = jarInputStream.getNextJarEntry()) != null) {
                if (ze.isDirectory()) {
                    continue;
                }
                if (ze.getName().contains(je.getName())) {
                    InputStream is = jf.getInputStream(ze);
                    String name = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                    File tmpFile = new File(tmpDir + "/" + name); //File.createTempFile(file.getName(), "tmp");
                    tmpFile.deleteOnExit();
                    OutputStream outputStreamRuntime = new FileOutputStream(tmpFile);
                    copyStream(is, outputStreamRuntime);
                }
            }
        }
    } finally {
        if (jf != null) {
            jf.close();
        }
        if (jarInputStream != null) {
            jarInputStream.close();
        }
    }
}

From source file:org.getobjects.foundation.NSJavaRuntime.java

private static void getClassNamesFromJarFile(String _jarPath, String _pkg, Set<String> classes_) {
    FileInputStream fos;/*from   w w  w  .  jav  a2s .  c o  m*/
    try {
        fos = new FileInputStream(_jarPath);
    } catch (FileNotFoundException e1) {
        return; // TBD: log
    }

    JarInputStream jarFile;
    try {
        jarFile = new JarInputStream(fos);
    } catch (IOException e) {
        return; // TBD: log
    }

    do {
        JarEntry jarEntry;
        try {
            jarEntry = jarFile.getNextJarEntry();
        } catch (IOException e) {
            jarEntry = null;
        }

        if (jarEntry == null)
            break;

        String className = jarEntry.getName();
        if (!className.endsWith(".class"))
            continue;

        className = stripFilenameExtension(className);
        if (className.startsWith(_pkg))
            classes_.add(className.replace('/', '.'));
    } while (true);

    if (jarFile != null) {
        try {
            jarFile.close();
        } catch (IOException e) {
            // TBD: log?
        }
    }
}

From source file:com.github.nullstress.asm.SourceSetScanner.java

public Set<String> analyzeJar(URL url) {
    Set<String> dependencies = new HashSet<String>();
    try {//  w w w  .ja va2s  . c  o m
        JarInputStream in = new JarInputStream(url.openStream());
        JarEntry entry;

        while ((entry = in.getNextJarEntry()) != null) {
            String name = entry.getName();

            if (name.endsWith(".class")) {
                dependencies.add(name.replaceAll("/", "."));
            }
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dependencies;
}

From source file:com.googlecode.arit.systest.Application.java

public File getExplodedWAR() {
    if (explodedWAR == null) {
        File explodedDir = new File(tmpDir, "exploded");
        explodedDir.mkdir();// w  ww .  j a v  a 2s .co  m
        String file = url.getFile();
        explodedWAR = new File(explodedDir, file.substring(file.lastIndexOf('/')));
        try {
            InputStream in = url.openStream();
            try {
                JarInputStream jar = new JarInputStream(in);
                JarEntry jarEntry;
                while ((jarEntry = jar.getNextJarEntry()) != null) {
                    File dest = new File(explodedWAR, jarEntry.getName());
                    if (jarEntry.isDirectory()) {
                        dest.mkdir();
                    } else {
                        dest.getParentFile().mkdirs();
                        OutputStream out = new FileOutputStream(dest);
                        try {
                            IOUtils.copy(jar, out);
                        } finally {
                            out.close();
                        }
                    }
                }
            } finally {
                in.close();
            }
        } catch (IOException ex) {
            throw new SystestException("Failed to explode WAR", ex);
        }
    }
    return explodedWAR;
}

From source file:org.commonjava.indy.ftest.core.content.StoreAndVerifyJarViaDirectDownloadTest.java

@Test
public void storeFileThenDownloadAndVerifyContentViaDirectDownload() throws Exception {
    final String content = "This is a test: " + System.nanoTime();
    String entryName = "org/something/foo.class";

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JarOutputStream jarOut = new JarOutputStream(out);
    jarOut.putNextEntry(new JarEntry(entryName));
    jarOut.write(content.getBytes());/*from w  w w .  ja  v a2s . co  m*/
    jarOut.close();

    // Used to visually inspect the jars moving up...
    //        String userDir = System.getProperty( "user.home" );
    //        File dir = new File( userDir, "temp" );
    //        dir.mkdirs();
    //
    //        FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-in.jar" ), out.toByteArray() );

    final InputStream stream = new ByteArrayInputStream(out.toByteArray());

    final String path = "/path/to/" + getClass().getSimpleName() + "-" + name.getMethodName() + ".jar";

    assertThat(client.content().exists(hosted, STORE, path), equalTo(false));

    client.content().store(hosted, STORE, path, stream);

    assertThat(client.content().exists(hosted, STORE, path), equalTo(true));

    final URL url = new URL(client.content().contentUrl(hosted, STORE, path));

    final InputStream is = url.openStream();

    byte[] result = IOUtils.toByteArray(is);
    is.close();

    assertThat(result, equalTo(out.toByteArray()));

    // ...and down
    //        FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-out.jar" ), result );

    JarInputStream jarIn = new JarInputStream(new ByteArrayInputStream(result));
    JarEntry jarEntry = jarIn.getNextJarEntry();

    assertThat(jarEntry.getName(), equalTo(entryName));
    String contentResult = IOUtils.toString(jarIn);

    assertThat(contentResult, equalTo(content));
}

From source file:ezbake.frack.submitter.util.JarUtilTest.java

@Test
public void addFileToJar() throws IOException {
    File tmpDir = new File("jarUtilTest");
    try {// w  w  w .  j  a v  a  2  s . c om
        tmpDir.mkdirs();

        // Push the example jar to a file
        byte[] testJar = IOUtils.toByteArray(this.getClass().getResourceAsStream("/example.jar"));
        File jarFile = new File(tmpDir, "example.jar");
        BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(jarFile));
        os.write(testJar);
        os.close();

        // Push the test content to a file
        byte[] testContent = IOUtils.toByteArray(this.getClass().getResourceAsStream("/test.txt"));
        String stringTestContent = new String(testContent);
        File testFile = new File(tmpDir, "test.txt");
        os = new BufferedOutputStream(new FileOutputStream(testFile));
        os.write(testContent);
        os.close();

        assertTrue(jarFile.exists());
        assertTrue(testFile.exists());

        // Add the new file to the jar
        File newJar = JarUtil.addFilesToJar(jarFile, Lists.newArrayList(testFile));
        assertTrue("New jar file exists", newJar.exists());
        assertTrue("New jar is a file", newJar.isFile());

        // Roll through the entries of the new jar and
        JarInputStream is = new JarInputStream(new FileInputStream(newJar));
        JarEntry entry = is.getNextJarEntry();
        boolean foundNewFile = false;
        String content = "";
        while (entry != null) {
            String name = entry.getName();
            if (name.endsWith("test.txt")) {
                foundNewFile = true;
                byte[] buffer = new byte[1];
                while ((is.read(buffer)) > 0) {
                    content += new String(buffer);
                }
                break;
            }
            entry = is.getNextJarEntry();
        }
        is.close();
        assertTrue("New file was in repackaged jar", foundNewFile);
        assertEquals("Content of added file is the same as the retrieved new file", stringTestContent, content);
    } finally {
        FileUtils.deleteDirectory(tmpDir);
    }
}

From source file:JarHelper.java

/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory.//from w ww  . j a v  a  2  s .c  o  m
 */
public void unjar(InputStream in, File destDir) throws IOException {
    BufferedOutputStream dest = null;
    JarInputStream jis = new JarInputStream(in);
    JarEntry entry;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(destDir, entry.getName());
            dir.mkdir();
            if (entry.getTime() != -1)
                dir.setLastModified(entry.getTime());
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];
        File destFile = new File(destDir, entry.getName());
        if (mVerbose)
            System.out.println("unjarring " + destFile + " from " + entry.getName());
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        if (entry.getTime() != -1)
            destFile.setLastModified(entry.getTime());
    }
    jis.close();
}

From source file:com.thoughtworks.go.domain.materials.tfs.TfsSDKCommandBuilder.java

private void explodeNatives() throws IOException {
    URL urlOfJar = getJarURL();/*from   ww w.  j  av a 2 s  .co  m*/
    LOGGER.info("[TFS SDK] Exploding natives from {} to folder {}", urlOfJar.toString(),
            tempFolder.getAbsolutePath());
    JarInputStream jarStream = new JarInputStream(urlOfJar.openStream());
    JarEntry entry;
    while ((entry = jarStream.getNextJarEntry()) != null) {
        if (!entry.isDirectory() && entry.getName().startsWith("tfssdk/native/")) {
            File newFile = new File(tempFolder, entry.getName());
            newFile.getParentFile().mkdirs();
            LOGGER.info("[TFS SDK] Extract {} -> {}", entry.getName(), newFile);
            try (OutputStream fos = new FileOutputStream(newFile)) {
                IOUtils.copy(jarStream, fos);
            }
        }
    }
}

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

public JarClassLoader(ClassLoader parentClassLoader, File jarFile) {
    super(parentClassLoader);
    this.jarFile = jarFile;
    try {/*from  w  ww. j  a  v  a 2s .co m*/
        JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile));
        JarEntry entry = jarInputStream.getNextJarEntry();
        map = new HashMap<String, byte[]>();
        while (entry != null) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            IOUtils.copy(jarInputStream, byteArrayOutputStream);
            map.put(entry.getName(), byteArrayOutputStream.toByteArray());
            if (entry.getName().endsWith(".jar")) {
                loadSubJars(byteArrayOutputStream.toByteArray());
            }
            entry = jarInputStream.getNextJarEntry();
        }
        jarInputStream.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}