如何从嵌套 Jar 中提取 .class 文件?

发布于 2024-11-03 02:56:57 字数 1347 浏览 0 评论 0原文

我有一个名为“OuterJar.jar”的 Jar 文件,其中包含另一个名为“InnerJar.jar”的 jar,此 InnerJar 包含 2 个名为“Test1.class”的文件强>”& “Test2.class”。现在我想提取这两个文件。我尝试了一些代码,但它不起作用。

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();

但问题是,“jarFile”obj 是 OuterJar 文件的 obj,因此尝试获取 InnerJar 中存在的文件的 inputStream 时是不可能的。

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();

But the problem is that the "jarFile" obj is the obj of OuterJar File so when trying to get the inputStream of a file that exists in the InnerJar is not possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山有枢 2024-11-10 02:56:57

您需要创建第二个 JarInputStream 来处理内部条目。
这就是你想要的:

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();
        }
    }
}

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();
        }
    }
}
年华零落成诗 2024-11-10 02:56:57

首先提取 InnerJar.jar,然后从中提取类文件。

Extract the InnerJar.jar first, then extract the class files from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文