0

I have jar containing different folder, sub-folder and files. To list and view the content of the jar i can use jar -tvf jar_name. Is there any way jar -tvf or similar other command which will list only directory inside content of jar.

I am seeing jar have following option but other than -t, I cant find which can help to list files or folder specifically.Not all in once with -t option.

jar Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
    -f  specify archive file name
    -m  include manifest information from specified manifest file
    -e  specify application entry point for stand-alone application
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -M  do not create a manifest file for the entries
    -i  generate index information for the specified jar files
    -C  change to the specified directory and include the following file

I need to load the jar files and display the folder and files in the tree structure. What could be the best approach implement such logic.

Gautam
  • 3,707
  • 5
  • 36
  • 57

2 Answers2

2

Command line(Linux);

jar tf JAR_PATH | grep ".*/$"

Java code to take directories in a jar file;

try {
    JarFile jarFile = new JarFile(JAR_PATH);
    Enumeration<JarEntry> paths = jarFile.entries();
    while (paths.hasMoreElements()) {
        JarEntry path = paths.nextElement();
        if (path.isDirectory()) {
            System.out.println(path.getName());
        }
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
1

Jar is a zip file. Use this answer . Or programatically answer1 or this answer2

Get a list of files (see docs) and grep those lines that end with "\" <-- you will get a list of directories.

Community
  • 1
  • 1