JarFile from inside a *.jar or inputstream to file?

后端 未结 4 1638
再見小時候
再見小時候 2021-01-13 23:02

I have a jar or war.

I\'m programmaticaly reading this jar, and when I find jar inside this jar I\'d like to programmaticaly read it again.

But JarFile prov

4条回答
  •  渐次进展
    2021-01-13 23:33

    Recursively use the JarFile again to read the new jar file. For ex.,

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Enumeration;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    import java.util.zip.ZipEntry;
    
    
    public class JarReader {
    
    public static void readJar(String jarName) throws IOException {
        String dir = new File(jarName).getParent();
        JarFile jf = new JarFile(jarName);
        Enumeration en = jf.entries();
        while(en.hasMoreElements()) { 
            ZipEntry ze = (ZipEntry)en.nextElement();
            if(ze.getName().endsWith(".jar")) { 
                readJar(dir + System.getProperty("file.separator") + ze.getName());
            } else {
                InputStream is = jf.getInputStream(ze);
                // ... read from input stream
                is.close();
                System.out.println("Processed class: " + ze.getName());
            }   
        }
    }
    
    public static void main(String[] args) throws IOException {
        readJar(args[0]);
    }
    
    }
    

提交回复
热议问题