如何从嵌套的Jar中提取.class文件? [英] How to extract .class files from nested Jar?
本文介绍了如何从嵌套的Jar中提取.class文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 OuterJar.jar 的Jar文件,其中包含另一个名为 InnerJar.jar 的jar,这个InnerJar包含2个名为 Test1的文件。课程& Test2.class 。现在我要提取这两个文件。我尝试了一些代码,但它不起作用。
I have a Jar file named "OuterJar.jar" that contains another jar named "InnerJar.jar" this InnerJar contains 2 files named "Test1.class" & "Test2.class".Now i want to extract these two files. I have tried some piece of code but it doesn't work.
class NestedJarExtractFactory{
public void nestedJarExtractor(String path){
JarFile jarFile = new JarFile(path);
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry _entryName = (JarEntry) entries.nextElement();
if(temp_FileName.endsWith(".jar")){
JarInputStream innerJarFileInputStream=new JarInputStream(jarFile.getInputStream(jarFile.getEntry(temp_FileName)));
System.out.println("Name of InnerJar Class Files::"+innerJarFileInputStream.getNextEntry());
JarEntry innerJarEntryFileName=innerJarFileInputStream.getNextJarEntry();
///////////Now hear I need some way to get the Input stream of this class file.After getting inputStream i just get that class obj through
JavaClass clazz = new ClassParser(InputStreamOfFile,"" ).parse();
}
///// I use the syntax
JavaClass clazz = new ClassParser(jarFile.getInputStream(innerJarEntryFileName),"" ).parse();
但问题是jarFileobj是OuterJar文件的obj所以当试图获取InnerJar中存在的文件的inputStream是不可能的。
推荐答案
你需要创建第二个 JarInputStream
来处理内部条目。
这样做你想要的:
You need to create a second JarInputStream
to process the inner entries.
This does what you want:
FileInputStream fin = new FileInputStream("OuterJar.jar");
JarInputStream jin = new JarInputStream(fin);
ZipEntry ze = null;
while ((ze = jin.getNextEntry()) != null) {
if (ze.getName().endsWith(".jar")) {
JarInputStream jin2 = new JarInputStream(jin);
ZipEntry ze2 = null;
while ((ze2 = jin2.getNextEntry()) != null) {
// this is bit of a hack to avoid stream closing,
// since you can't get one for the inner entry
// because you have no JarFile to get it from
FilterInputStream in = new FilterInputStream(jin2) {
public void close() throws IOException {
// ignore the close
}
};
// now you can process the input stream as needed
JavaClass clazz = new ClassParser(in, "").parse();
}
}
}
这篇关于如何从嵌套的Jar中提取.class文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文