2011-04-26 3 views
3

내가 "InnerJar.jar"라는 이름의 또 다른 병이 들어 "OuterJar.jar"라는 이름의 JAR 파일이이 InnerJar는 "Test1.class"& "Test2.class"부탁해라는 이름의이 개 파일이 포함되어 있습니다 이 두 파일을 추출하고 싶습니다. 일부 코드를 시도했지만 작동하지 않습니다.중첩 된 Jar에서 .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(); 

는 그러나 문제는 InnerJar에 존재하는 파일의 inputStream을 얻을하려고 할 때 OuterJar의 obj가 너무 파일입니다 OBJ은 "jarfile가"이 불가능하다는 것이다.

답변

4

두 번째 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(); 
     } 
    } 
} 
+0

이 WhiteFang34 @ 솔루션 그래서 먼저 내가 JarEntry의의의 InputStream이 필요합니다 내가 FileOutputStream에 통해 다른 파일을 만들지 않으 2ndly 매우 much.Now 감사 나에게 80 % 도움 : 이것은 당신이 원하는 것을 . hear 귀하의 코드에서 나는 ze2의 inputStream을 얻을 필요가있다. 그래서 조금 더 도움이됩니다. – zaree

+0

@ 자리 : 문제 없습니다. 나의 예제에서는 내부 'ze2' 항목에 대해'InputStream'으로'jin2'를 사용하려고합니다. 나는 그것을 보여주고 테스트하기 위해 다른 파일에만 썼다. – WhiteFang34

+0

@ WhiteFang34 먼저 InputStream을 인스턴스화 할 수 없으며 InputStream = new FileInputStream (ze2.getName(). toStreing())과 같은 작업을 수행한다고 말할 수 있습니다. 그리고 나서 jin2를 취하는 것이 JarInputStream 대신 FileInputStream이되는 경우에는 jar 파일을 열어 jar 파일을 열어 파일 인 innerjar 항목을 가져 와서 정확한 위치에 도달하지 못하게해야합니다 포인트! 친절하게 코드의 UR 저격을 업데이 트하십시오. – zaree

2

InnerJar.jar의 압축을 푼 다음 클래스 파일의 압축을 풉니 다.

관련 문제