Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4004930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:19:58+00:00 2026-05-20T08:19:58+00:00

My question is relativly simple. Does anybody know a free library (LGPL) that is

  • 0

My question is relativly simple. Does anybody know a free library (LGPL) that is capable of unzipping a zipped file structure into a TreeMap (or a similar iteratable structure) for Java?

Point is, that I could do this myself, but I don’t want to reinvent already good riding wheels πŸ™‚

Thanks in advance!


So my thing is, that i have a zip file, containing multiple files and directories which may contain more files. What i am looking for is a convinient way how to extract this tree structure into an object graph, whether it’s a TreeMap or not. So for example: a HashMap : {'root' => 'HashMap : {'file1.png' => byte[] content}}

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:19:59+00:00Added an answer on May 20, 2026 at 8:19 am

    So my thing is, that i have a zip file, containing multiple files and directories which may contain more files.
    What i am looking for is a convinient way how to extract this tree structure into an object graph, whether it’s a TreeMap or not.
    So for example: a HashMap : {‘root’, ‘HashMap : {‘file1.png’ => byte[] content}}

    As I answered some time ago on another question, there is no single “tree” data structure (tree interface) in the Java API, since every use needs other features.
    Your proposed HashMap-tree, for example, is not realizable in a type-save way – you would need wrapper objects somewhere.

    I don’t know if somewhere out there is already a tree-like view of a zip file, to (not) answer your question, but it is not really difficult to create as soon as you define your wished tree interface.


    So, here is an example class which does what you want (from what I understand).

    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    
    /**
     * A immutable wrapper around {@link ZipEntry} allowing
     * simple access of the childs of directory entries.
     */
    public class ZipNode {
    
        private ZipNode parent;
        private Map<String,ZipNode> children;
        private boolean directory;
    
        /**
         * the corresponding Zip entry. If null, this is the root entry.
         */
        private ZipEntry entry;
    
        /** the ZipFile from where the nodes came. */
        private ZipFile file;
    
    
        private ZipNode(ZipFile f, ZipEntry entry) {
            this.file = f;
            this.entry = entry;
            if(entry == null || entry.isDirectory()) {
                directory = true;
                children = new LinkedHashMap<String, ZipNode>();
            }
            else {
                directory = false;
                children = Collections.emptyMap();
            }
        }
    
        /**
         * returns the last component of the name of
         * the entry, i.e. the file name. If this is a directory node,
         * the name ends with '/'. If this is the root node, the name
         * is simply "/".
         */
        public String getName() {
            if(entry == null)
                return "/";
            String longName = entry.getName();
            return longName.substring(longName.lastIndexOf('/',
                                                           longName.length()-2)+1);
        }
    
        /**
         * gets the corresponding ZipEntry to this node.
         * @return {@code null} if this is the root node (which has no
         *    corresponding entry), else the corresponding ZipEntry.
         */
        public ZipEntry getEntry() {
            return entry;
        }
    
        /**
         * Gets the ZipFile, from where this ZipNode came.
         */
        public ZipFile getZipFile() {
            return file;
        }
    
        /**
         * returns true if this node is a directory node.
         */
        public boolean isDirectory() {
            return directory;
        }
    
        /**
         * returns this node's parent node (null, if this is the root node).
         */
        public ZipNode getParent() {
            return parent;
        }
    
        /**
         * returns an unmodifiable map of the children of this node,
         * mapping their relative names to the ZipNode objects.
         * (Names of subdirectories end with '/'.)
         * The map is empty if this node is not a directory node.
         */
        public Map<String,ZipNode> getChildren() {
            return Collections.unmodifiableMap(children);
        }
    
        /**
         * opens an InputStream on this ZipNode. This only works when
         * this is not a directory node, and only before the corresponding
         * ZipFile is closed.
         */
        public InputStream openStream()
            throws IOException
        {
            return file.getInputStream(entry);
        }
    
        /**
         * a string representation of this ZipNode.
         */
        public String toString() {
            return "ZipNode [" + entry.getName() + "] in [" + file.getName() + "]";
        }
    
    
    
        /**
         * creates a ZipNode tree from a ZipFile
         * and returns the root node.
         *
         * The nodes' {@link #openStream()} methods are only usable until the
         * ZipFile is closed, but the structure information remains valid.
         */
        public static ZipNode fromZipFile(ZipFile zf) {
            return new ZipFileReader(zf).process();
        }
    
    
        /**
         * Helper class for {@link ZipNode#fromZipFile}.
         * It helps creating a tree of ZipNodes from a ZipFile.
         */
        private static class ZipFileReader {
            /**
             * The file to be read.
             */
            private ZipFile file;
    
            /**
             * The nodes collected so far.
             */
            private Map<String, ZipNode> collected;
    
            /**
             * our root node.
             */
            private ZipNode root;
    
            /**
             * creates a new ZipFileReader from a ZipFile.
             */
            ZipFileReader(ZipFile f) {
                this.file = f;
                this.collected = new HashMap<String, ZipNode>();
                collected.put("", root);
                root = new ZipNode(f, null);
            }
    
            /**
             * reads all entries, creates the corresponding Nodes and
             * returns the root node.
             */
            ZipNode process() {
                for(Enumeration<? extends ZipEntry> entries = file.entries();
                    entries.hasMoreElements(); ) {
                    this.addEntry(entries.nextElement());
                }
                return root;
            }
    
            /**
             * adds an entry to our tree.
             *
             * This may create a new ZipNode and then connects
             * it to its parent node.
             * @returns the ZipNode corresponding to the entry.
             */
            private ZipNode addEntry(ZipEntry entry) {
                String name = entry.getName();
                ZipNode node = collected.get(name);
                if(node != null) {
                    // already in the map
                    return node;
                }
                node = new ZipNode(file, entry);
                collected.put(name, node);
                this.findParent(node);
                return node;
            }
    
            /**
             * makes sure that the parent of a
             * node is in the collected-list as well, and this node is
             * registered as a child of it.
             * If necessary, the parent node is first created
             * and added to the tree.
             */
            private void findParent(ZipNode node) {
                String nodeName = node.entry.getName();
                int slashIndex = nodeName.lastIndexOf('/', nodeName.length()-2);
                if(slashIndex < 0) {
                    // top-level-node
                    connectParent(root, node, nodeName);
                    return;
                }
                String parentName = nodeName.substring(0, slashIndex+1);
                ZipNode parent = addEntry(file.getEntry(parentName));
                connectParent(parent, node, nodeName.substring(slashIndex+1));
            }
    
            /**
             * connects a parent node with its child node.
             */
            private void connectParent(ZipNode parent, ZipNode child,
                                       String childName) {
                child.parent = parent;
                parent.children.put(childName, child);
            }
    
    
        }  // class ZipFileReader
    
        /**
         * test method. Give name of zip file as command line argument.
         */
        public static void main(String[] params)
            throws IOException
        {
            if(params.length < 1) {
                System.err.println("Invocation:  java ZipNode zipFile.zip");
                return;
            }
            ZipFile file = new ZipFile(params[0]);
            ZipNode root = ZipNode.fromZipFile(file);
            file.close();
            root.printTree("", " ", "");
        }
    
        /**
         * prints a simple tree view of this ZipNode.
         */
        private void printTree(String prefix,
                               String self,
                               String sub) {
            System.out.println(prefix + self + this.getName());
            String subPrefix = prefix + sub;
            // the prefix strings for the next level.
            String nextSelf = " β”œβ”€ ";
            String nextSub =  " β”‚ ";
            Iterator<ZipNode> iterator =
                this.getChildren().values().iterator();
            while(iterator.hasNext()) {
                ZipNode child = iterator.next();
                if(!iterator.hasNext() ) {
                    // last item, without the "|"
                    nextSelf = " ╰─ ";
                    nextSub =  "   ";
                }
                child.printTree(subPrefix, nextSelf, nextSub);
            }
        }
    }
    

    It has a main method for test use, its output for one of my jar files is this:

     /
     β”œβ”€ META-INF/
     β”‚  ╰─ MANIFEST.MF
     ╰─ de/
        ╰─ fencing_game/
           β”œβ”€ start/
           β”‚  β”œβ”€ Runner.class
           β”‚  β”œβ”€ ServerMain$1.class
           β”‚  β”œβ”€ ServerMain.class
           β”‚  ╰─ package-info.class
           β”œβ”€ log/
           β”‚  β”œβ”€ Log$1.class
           β”‚  β”œβ”€ Log.class
           β”‚  β”œβ”€ LogImplClient.class
           β”‚  β”œβ”€ Loggable.class
           β”‚  ╰─ package-info.class
           ╰─ tools/
              ╰─ load/
                 β”œβ”€ ServiceTools$1.class
                 β”œβ”€ ServiceTools$2.class
                 β”œβ”€ ServiceTools$3.class
                 β”œβ”€ ServiceTools.class
                 ╰─ TwoParentClassLoader.class
    

    (You’ll need an unicode-capable Terminal and an Unicode encoding for System.out, though.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple question. Will using PHP guarantee that a form is
I have a relatively simple question that I cannot seem to find the answer
A relatively simple question. I have a datagridview, which all it does is displays
I have a relatively simple question that I cant figure out and I cant
simple question: Does adding something like this to a query hurt performance in mysql
This is a relatively simple question, but I haven't been able to find a
I've got a relatively simple question about asp.net MVC models. I've got a model
I have a question, and I'm sure there is a relatively simple answer to
Just a simple question from a relative Java newbie: what is the difference between
I've got a relatively simple Cocoa on Mac OS X 10.6 question to ask.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.