'How to copy a JarEntry to a directory

I have scanned the maven directory (.m2) and have filtered all the files ending with .jar.

List<File> jarFiles = scanRecursivelyForJarObjects(mavenRepository, fileManager);

I am trying the copy the .class files inside the JarFile to a different directory. Here is the code:

for(File jarFile : jarFiles) {
        Enumeration<JarEntry> enumeration = new JarFile(jarFile).entries();
        while (enumeration.hasMoreElements()) {
            JarEntry jarEntry = enumeration.nextElement();
            if (!jarEntry.isDirectory() && jarEntry.getName().endsWith(".class")) {
                copyFile(new FileInputStream(jarEntry), classesDir.getAbsolutePath() + "\\" + jarEntry.getName());
            }
        }
    }

But it is throwing an error when I try to copy because jarEntry is not a File.

I am not sure how to convert jarEntry to a File.



Solution 1:[1]

You can get the input stream to the entry.

new JarFile(file).getInputStream(jarEntry)

Solution 2:[2]

IOUtils.copyStream(jarEntry.getInputStream(), new FileOutputStream(new File(classesDir.getAbsolutePath(), jarEntry.getName())));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravi Gupta
Solution 2 g00se