0

Solutions(Get list of classes in package , How do I list the files inside a JAR file? , Listing the files in a directory of the current JAR file) are all used jarEntry to list classes in a jar file, however, jarEntry only can be used as level traverse. What should I do when I want to traverse classes and folders in a jar file using depth-first-traverse? Thank you for your consideration.

FIXED

ENV

jdk 1.6

TRIED

Add into the tree

static void getClasses(File pareFile, TJtree root){
      TJtree parent, temp;
      parent = root;

    ArrayList<String> asArrayList = new ArrayList<String>();

    File f = pareFile;
    if(f.exists()){
        try{
            JarInputStream jarFile = new JarInputStream(new FileInputStream(f));

            JarEntry jarEntry;
            String [] jarEntryString;
            String jarNodeNameString;

            while(true) {
                jarEntry=jarFile.getNextJarEntry();  

                if(jarEntry == null){
                    break;
                }

                asArrayList.add(jarEntry.getName());
                Collections.sort(asArrayList);

                jarEntryString = jarEntry.getName().split("/");
                jarNodeNameString = jarEntryString[jarEntryString.length-1];

                if(jarEntry.isDirectory()) {
                    temp = new TJtree();
                    temp.data = jarNodeNameString;
                    parent.childlist.add(temp);
                    parent = temp;
                } else {
                    if(jarEntry.getName().endsWith (".class")) {
                        temp = new TJtree();
                        temp.data = jarNodeNameString;
                        parent.childlist.add(temp);

                    }
                }
          }
                for (String tmpString : asArrayList) {
                    System.out.println("++"+tmpString);
                }
       } catch( Exception e){
            e.printStackTrace ();
       }
    }
}

Display the tree

  static void displaytree2(TJtree f , int level) {  

        String preStr = "";  
        for(int i=0; i<level; i++) {  
         preStr += "    ";  
        }  

        for(int i=0; i<f.childlist.size(); i++) {  
            TJtree t = f.childlist.get(i);  
            System.out.println(preStr + "-"+t.data);  

         if(! t.childlist.isEmpty()) {  
             displaytree2(t, level + 1);  
         }  
        }  
   } 

output

-org.objectweb.asm_3.3.1.v201105211655.jar
    -META-INF
        -about_files
            -org
                -objectweb
                    -asm
                        -commons
                            -signature
                                -tree
                                    -analysis
                                        -util
                                            -xml
                                                -AnnotationVisitor.class
                                                -AnnotationWriter.class
                                                -Attribute.class
                                                -ByteVector.class
                                                -ClassAdapter.class
                                                -ClassReader.class
                                                -ClassVisitor.class
                                                -ClassWriter.class
                                                -Edge.class
                                                -FieldVisitor.class
                                                -FieldWriter.class
                                                -Frame.class
                                                -Handler.class
                                                -Item.class
                                                -Label.class
                                                -MethodAdapter.class
                                                -MethodVisitor.class
                                                -MethodWriter.class
                                                -Opcodes.class
                                                -Type.class
                                                -AdviceAdapter.class
                                                -AnalyzerAdapter.class
                                                -CodeSizeEvaluator.class
                                                -EmptyVisitor.class
                                                -GeneratorAdapter.class
                                                -InstructionAdapter.class
                                                -JSRInlinerAdapter$Instantiation.class
                                                -JSRInlinerAdapter$Subroutine.class
                                                -JSRInlinerAdapter.class
                                                -LocalVariablesSorter.class
                                                -Method.class
                                                -Remapper.class
                                                -RemappingAnnotationAdapter.class
                                                -RemappingClassAdapter.class
                                                -RemappingFieldAdapter.class
                                                -RemappingMethodAdapter.class
                                                -RemappingSignatureAdapter.class
                                                -SerialVersionUIDAdder$Item.class
                                                -SerialVersionUIDAdder.class
                                                -SimpleRemapper.class
                                                -StaticInitMerger.class
                                                -TableSwitchGenerator.class
                                                -TryCatchBlockSorter$1.class
                                                -TryCatchBlockSorter.class
                                                -SignatureReader.class
                                                -SignatureVisitor.class
                                                -SignatureWriter.class
                                                -AbstractInsnNode.class
                                                -AnnotationNode.class
                                                -ClassNode.class
                                                -FieldInsnNode.class
                                                -FieldNode.class
                                                -FrameNode.class
                                                -IincInsnNode.class
                                                -InnerClassNode.class
                                                -InsnList$InsnListIterator.class
                                                -InsnList.class
                                                -InsnNode.class
                                                -IntInsnNode.class
                                                -JumpInsnNode.class
                                                -LabelNode.class
                                                -LdcInsnNode.class
                                                -LineNumberNode.class
                                                -LocalVariableNode.class
                                                -LookupSwitchInsnNode.class
                                                -MemberNode.class
                                                -MethodInsnNode.class
                                                -MethodNode$1.class
                                                -MethodNode.class
                                                -MultiANewArrayInsnNode.class
                                                -TableSwitchInsnNode.class
                                                -TryCatchBlockNode.class
                                                -TypeInsnNode.class
                                                -VarInsnNode.class
                                                -Analyzer.class
                                                -AnalyzerException.class
                                                -BasicInterpreter.class
                                                -BasicValue.class
                                                -BasicVerifier.class
                                                -Frame.class
                                                -Interpreter.class
                                                -SimpleVerifier.class
                                                -SmallSet.class
                                                -SourceInterpreter.class
                                                -SourceValue.class
                                                -Subroutine.class
                                                -Value.class
                                                -ASMifiable.class
                                                -ASMifierAbstractVisitor.class
                                                -ASMifierAnnotationVisitor.class
                                                -ASMifierClassVisitor.class
                                                -ASMifierFieldVisitor.class
                                                -ASMifierMethodVisitor.class
                                                -AbstractVisitor.class
                                                -CheckAnnotationAdapter.class
                                                -CheckClassAdapter.class
                                                -CheckFieldAdapter.class
                                                -CheckMethodAdapter$1.class
                                                -CheckMethodAdapter.class
                                                -CheckSignatureAdapter.class
                                                -TraceAbstractVisitor.class
                                                -TraceAnnotationVisitor.class
                                                -TraceClassVisitor.class
                                                -TraceFieldVisitor.class
                                                -TraceMethodVisitor.class
                                                -TraceSignatureVisitor.class
                                                -Traceable.class
                                                -ASMContentHandler$AnnotationDefaultRule.class
                                                -ASMContentHandler$AnnotationParameterRule.class
                                                -ASMContentHandler$AnnotationRule.class
                                                -ASMContentHandler$AnnotationValueAnnotationRule.class
                                                -ASMContentHandler$AnnotationValueArrayRule.class
                                                -ASMContentHandler$AnnotationValueEnumRule.class
                                                -ASMContentHandler$AnnotationValueRule.class
                                                -ASMContentHandler$ClassRule.class
                                                -ASMContentHandler$ExceptionRule.class
                                                -ASMContentHandler$ExceptionsRule.class
                                                -ASMContentHandler$FieldRule.class
                                                -ASMContentHandler$FrameRule.class
                                                -ASMContentHandler$FrameTypeRule.class
                                                -ASMContentHandler$InnerClassRule.class
                                                -ASMContentHandler$InterfaceRule.class
                                                -ASMContentHandler$InterfacesRule.class
                                                -ASMContentHandler$LabelRule.class
                                                -ASMContentHandler$LineNumberRule.class
                                                -ASMContentHandler$LocalVarRule.class
                                                -ASMContentHandler$LookupSwitchLabelRule.class
                                                -ASMContentHandler$LookupSwitchRule.class
                                                -ASMContentHandler$MaxRule.class
                                                -ASMContentHandler$MethodRule.class
                                                -ASMContentHandler$Opcode.class
                                                -ASMContentHandler$OpcodeGroup.class
                                                -ASMContentHandler$OpcodesRule.class
                                                -ASMContentHandler$OuterClassRule.class
                                                -ASMContentHandler$Rule.class
                                                -ASMContentHandler$RuleSet.class
                                                -ASMContentHandler$SourceRule.class
                                                -ASMContentHandler$TableSwitchLabelRule.class
                                                -ASMContentHandler$TableSwitchRule.class
                                                -ASMContentHandler$TryCatchRule.class
                                                -ASMContentHandler.class
                                                -Processor$ASMContentHandlerFactory.class
                                                -Processor$ContentHandlerFactory.class
                                                -Processor$EntryElement.class
                                                -Processor$InputSlicingHandler.class
                                                -Processor$OutputSlicingHandler.class
                                                -Processor$ProtectedInputStream.class
                                                -Processor$SAXWriter.class
                                                -Processor$SAXWriterFactory.class
                                                -Processor$SingleDocElement.class
                                                -Processor$SubdocumentHandlerFactory.class
                                                -Processor$TransformerHandlerFactory.class
                                                -Processor$ZipEntryElement.class
                                                -Processor.class
                                                -SAXAdapter.class
                                                -SAXAnnotationAdapter.class
                                                -SAXClassAdapter.class
                                                -SAXCodeAdapter.class
                                                -SAXFieldAdapter.class

EXPECTED INPUT

If input a path of a jar file(e.g. org.objectweb.asm_3.3.1.v201105211655.jar)

EXPECTED OUTPUT

The output should be the files structure(It will be better to just include class files and folders containing class files.) in it.

D:\WORK\WORKSPACE\MYJAR\CC\ORG.OBJECTWEB.ASM_3.3.1.V201105211655
│  about.html
│  plugin.properties
│
├─about_files
│      LICENSE.txt
│      README.txt
│
├─META-INF
│      eclipse.inf
│      ECLIPSEF.RSA
│      ECLIPSEF.SF
│      MANIFEST.MF
│
└─org
    └─objectweb
        └─asm
            │  AnnotationVisitor.class
            │  AnnotationWriter.class
            │  Attribute.class
            │  ByteVector.class
            │  ClassAdapter.class
            │  ClassReader.class
            │  ClassVisitor.class
            │  ClassWriter.class
            │  Edge.class
            │  FieldVisitor.class
            │  FieldWriter.class
            │  Frame.class
            │  Handler.class
            │  Item.class
            │  Label.class
            │  MethodAdapter.class
            │  MethodVisitor.class
            │  MethodWriter.class
            │  Opcodes.class
            │  Type.class
            │
            ├─commons
            │      AdviceAdapter.class
            │      AnalyzerAdapter.class
            │      CodeSizeEvaluator.class
            │      EmptyVisitor.class
            │      GeneratorAdapter.class
            │      InstructionAdapter.class
            │      JSRInlinerAdapter$Instantiation.class
            │      JSRInlinerAdapter$Subroutine.class
            │      JSRInlinerAdapter.class
            │      LocalVariablesSorter.class
            │      Method.class
            │      Remapper.class
            │      RemappingAnnotationAdapter.class
            │      RemappingClassAdapter.class
            │      RemappingFieldAdapter.class
            │      RemappingMethodAdapter.class
            │      RemappingSignatureAdapter.class
            │      SerialVersionUIDAdder$Item.class
            │      SerialVersionUIDAdder.class
            │      SimpleRemapper.class
            │      StaticInitMerger.class
            │      TableSwitchGenerator.class
            │      TryCatchBlockSorter$1.class
            │      TryCatchBlockSorter.class
            │
            ├─signature
            │      SignatureReader.class
            │      SignatureVisitor.class
            │      SignatureWriter.class
            │
            ├─tree
            │  │  AbstractInsnNode.class
            │  │  AnnotationNode.class
            │  │  ClassNode.class
            │  │  FieldInsnNode.class
            │  │  FieldNode.class
            │  │  FrameNode.class
            │  │  IincInsnNode.class
            │  │  InnerClassNode.class
            │  │  InsnList$InsnListIterator.class
            │  │  InsnList.class
            │  │  InsnNode.class
            │  │  IntInsnNode.class
            │  │  JumpInsnNode.class
            │  │  LabelNode.class
            │  │  LdcInsnNode.class
            │  │  LineNumberNode.class
            │  │  LocalVariableNode.class
            │  │  LookupSwitchInsnNode.class
            │  │  MemberNode.class
            │  │  MethodInsnNode.class
            │  │  MethodNode$1.class
            │  │  MethodNode.class
            │  │  MultiANewArrayInsnNode.class
            │  │  TableSwitchInsnNode.class
            │  │  TryCatchBlockNode.class
            │  │  TypeInsnNode.class
            │  │  VarInsnNode.class
            │  │
            │  └─analysis
            │          Analyzer.class
            │          AnalyzerException.class
            │          BasicInterpreter.class
            │          BasicValue.class
            │          BasicVerifier.class
            │          Frame.class
            │          Interpreter.class
            │          SimpleVerifier.class
            │          SmallSet.class
            │          SourceInterpreter.class
            │          SourceValue.class
            │          Subroutine.class
            │          Value.class
            │
            ├─util
            │      AbstractVisitor.class
            │      ASMifiable.class
            │      ASMifierAbstractVisitor.class
            │      ASMifierAnnotationVisitor.class
            │      ASMifierClassVisitor.class
            │      ASMifierFieldVisitor.class
            │      ASMifierMethodVisitor.class
            │      CheckAnnotationAdapter.class
            │      CheckClassAdapter.class
            │      CheckFieldAdapter.class
            │      CheckMethodAdapter$1.class
            │      CheckMethodAdapter.class
            │      CheckSignatureAdapter.class
            │      Traceable.class
            │      TraceAbstractVisitor.class
            │      TraceAnnotationVisitor.class
            │      TraceClassVisitor.class
            │      TraceFieldVisitor.class
            │      TraceMethodVisitor.class
            │      TraceSignatureVisitor.class
            │
            └─xml
                    ASMContentHandler$AnnotationDefaultRule.class
                    ASMContentHandler$AnnotationParameterRule.class
                    ASMContentHandler$AnnotationRule.class
                    ASMContentHandler$AnnotationValueAnnotationRule.class
                    ASMContentHandler$AnnotationValueArrayRule.class
                    ASMContentHandler$AnnotationValueEnumRule.class
                    ASMContentHandler$AnnotationValueRule.class
                    ASMContentHandler$ClassRule.class
                    ASMContentHandler$ExceptionRule.class
                    ASMContentHandler$ExceptionsRule.class
                    ASMContentHandler$FieldRule.class
                    ASMContentHandler$FrameRule.class
                    ASMContentHandler$FrameTypeRule.class
                    ASMContentHandler$InnerClassRule.class
                    ASMContentHandler$InterfaceRule.class
                    ASMContentHandler$InterfacesRule.class
                    ASMContentHandler$LabelRule.class
                    ASMContentHandler$LineNumberRule.class
                    ASMContentHandler$LocalVarRule.class
                    ASMContentHandler$LookupSwitchLabelRule.class
                    ASMContentHandler$LookupSwitchRule.class
                    ASMContentHandler$MaxRule.class
                    ASMContentHandler$MethodRule.class
                    ASMContentHandler$Opcode.class
                    ASMContentHandler$OpcodeGroup.class
                    ASMContentHandler$OpcodesRule.class
                    ASMContentHandler$OuterClassRule.class
                    ASMContentHandler$Rule.class
                    ASMContentHandler$RuleSet.class
                    ASMContentHandler$SourceRule.class
                    ASMContentHandler$TableSwitchLabelRule.class
                    ASMContentHandler$TableSwitchRule.class
                    ASMContentHandler$TryCatchRule.class
                    ASMContentHandler.class
                    Processor$ASMContentHandlerFactory.class
                    Processor$ContentHandlerFactory.class
                    Processor$EntryElement.class
                    Processor$InputSlicingHandler.class
                    Processor$OutputSlicingHandler.class
                    Processor$ProtectedInputStream.class
                    Processor$SAXWriter.class
                    Processor$SAXWriterFactory.class
                    Processor$SingleDocElement.class
                    Processor$SubdocumentHandlerFactory.class
                    Processor$TransformerHandlerFactory.class
                    Processor$ZipEntryElement.class
                    Processor.class
                    SAXAdapter.class
                    SAXAnnotationAdapter.class
                    SAXClassAdapter.class
                    SAXCodeAdapter.class
                    SAXFieldAdapter.class

EDIT

structure I used which I want to store the result:

    class {
    String data;  
    TJtree parent;   //treenode
    LinkedList<TJtree> childlist; 
    }
Community
  • 1
  • 1
Nick Dong
  • 3,638
  • 8
  • 47
  • 84
  • 1
    Basically - use a [stack](http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29) instead of a queue when extracting entries. Will help if you can reference to the breadth-first solution you had in mind – amit Nov 01 '13 at 08:52
  • how does the stack know the relationships between parent entries and sub-entries? – Nick Dong Nov 01 '13 at 08:54
  • Why do you want to do this? What difference will it make as a class can only be in one package so how you get there doesn't matter. – Peter Lawrey Nov 01 '13 at 08:54
  • @NickDong It doesn't, you only asked how to change the order of traversal. If you what this you will have to add it too. – Peter Lawrey Nov 01 '13 at 08:55
  • I need to output the class struture(include floders in jar) as a tree. – Nick Dong Nov 01 '13 at 08:55
  • So I would look at all the entries of the JAR, building a tree and print it out at the end. – Peter Lawrey Nov 01 '13 at 08:56
  • sorry for late. how to do that @Peter Lawrey? – Nick Dong Nov 01 '13 at 10:28
  • 1
    Read the entries in the order they appear. Using the full path name add them to a TreeMap. This will sort them and arrange them the way you want. To display as a tree, iterate over the full path names looking to see when the directory changes and display appropriately. – Peter Lawrey Nov 01 '13 at 10:33
  • I am not mean how to change. I mean how to do in depth first traverse. I have tried to use `jarEntry`, but It seems only can be used as level traverse. I need Any solutions to do in depth first traverse. – Nick Dong Nov 01 '13 at 10:33
  • The order you get them is the order they were stored. If you need another order you need to build a data structure. – Peter Lawrey Nov 01 '13 at 10:36
  • I was build a structure. `String data; TJtree parent; LinkedList childlist; ` See **EDIT** in question description. – Nick Dong Nov 01 '13 at 10:37
  • Is it clear about my question now? @Holger – Nick Dong Nov 04 '13 at 02:47
  • your comment can be treated as an answer, It is helpful. @Peter Lawrey – Nick Dong Nov 08 '13 at 07:34

1 Answers1

1

Read the entries in the order they appear. Using the full path name add them to a TreeMap. This will sort them and arrange them the way you want. To display as a tree, iterate over the full path names looking to see when the directory changes and display appropriately.

e.g.

final NavigableMap<String, MyData> entryMap = new TreeMap<>();

// add each entry by full path to the entryMap
entryMap.put(fullpath, new MyData(... ... ...));

// later process the entries in the order required
File lastDirectory = new File("/");
for(Map.Enrty<String, MyData> entry : entryMap.entrySet()) {
   File file = new File(entry.getKey());
   MyData myData = entry.getValue();
   if (!file.getParent().equals(lastDirectory.getParent()) {
        // different directory
   }
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    I store them into a list, and sort them. And then treat them as strings with full path. Then split these strings to tree nodes. Your idea is helpful. thx – Nick Dong Nov 12 '13 at 09:33