This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes zip to JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "zip to JTree" Watch "zip to JTree" New topic
Author

zip to JTree

Iban Cardona
Greenhorn

Joined: Apr 15, 2009
Posts: 9
Hello,

Anybody know any method to explore a zip file and convert their content to a JTree?

Thank you very much.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I assume you know how to read all entries in a Zip file.

I would keep a mapping from entry names to DefaultMutableTreeNodes. Then, when you encounter a ZipEntry, you determine its parent from the name (using the / as the separator*). You retrieve the parent node from the map, creating it if necessary. Then you create a DefaultMutableTreeNode, and add it to the parent node. Finally you add the new mapping.

In pseudo code (untested):

* You can be lazy and use java.io.File for this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Iban Cardona
Greenhorn

Joined: Apr 15, 2009
Posts: 9
Rob Prime wrote:I assume you know how to read all entries in a Zip file.

I would keep a mapping from entry names to DefaultMutableTreeNodes. Then, when you encounter a ZipEntry, you determine its parent from...


Thank you.

Using split would be a good solution for the name of the parent?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

If you can get the right regex, sure. Not that that's too hard
Iban Cardona
Greenhorn

Joined: Apr 15, 2009
Posts: 9
George,

Many thanks. Helped me a lot.
I made a small test and seems to work pretty well. I show you the code adapted to my needs.



What do you think?
Many thanks again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

This will cause problems if you happen to have a zip entry with the same name as the value of nomFitxer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: zip to JTree
 
Similar Threads
Code for JTree with checkbox
JTree Root Icon
JTree
Display content of zip in JTree?
Java's JTree (TreePath)