Java URL Connection jarURLDirectories(URL jarURL)

Here you can find the source of jarURLDirectories(URL jarURL)

Description

Return the directories in a jar URI, relative to the jar entry, if any.

License

Open Source License

Parameter

Parameter Description
jarURL The Jar URL for which we are to look for directories.

Return

An list of Strings that name the directories

Declaration

public static List jarURLDirectories(URL jarURL) throws IOException 

Method Source Code

//package com.java2s;
/* Utilities used to manipulate classes
    /*from   www. j  ava  2 s .c  o m*/
 Copyright (c) 2003-2011 The Regents of the University of California.
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.
    
 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
    
 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.
    
 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
    
 */

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    /** Return the directories in a jar URI, relative to the jar entry,
     *  if any.   .
     *  Jar URLS have several forms, the most common being:
     *  <code>jar:file:///foo/bar.jar/!/bif/baz</code>, which means that
     *  the jar file /foo/bar.jar has a directory or file name bif/baz.
     *  If such a file is passed to this method, then any directories
     *  in the jar file directory bif/baz will be returned.
     *  @param jarURL The Jar URL for which we are to look for directories.
     *  @return An list of Strings that name the directories
     *  @exception IOException If opening the connection fails or if
     *  getting the jar file from the connection fails
     */
    public static List jarURLDirectories(URL jarURL) throws IOException {
        List directories = new LinkedList();
        JarURLConnection connection = (JarURLConnection) (jarURL.openConnection());
        String jarEntryName = connection.getEntryName();
        if (jarEntryName.endsWith("/")) {
            jarEntryName = jarEntryName.substring(0, jarEntryName.length() - 1);
        }
        JarFile jarFile = connection.getJarFile();
        Enumeration entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            String name = entry.getName();
            int jarEntryIndex = name.indexOf(jarEntryName + "/");
            int jarEntrySlashIndex = jarEntryIndex + jarEntryName.length() + 1;

            int nextSlashIndex = name.indexOf("/", jarEntrySlashIndex);
            int lastSlashIndex = name.indexOf("/", jarEntrySlashIndex);

            if (jarEntryIndex > -1 && jarEntrySlashIndex > -1 && nextSlashIndex > -1
                    && nextSlashIndex == lastSlashIndex && nextSlashIndex == name.length() - 1
                    && entry.isDirectory()) {
                directories.add(name);
            }
        }
        return directories;
    }
}

Related

  1. isOnline(String url)
  2. isUrlReachable(String url)
  3. isValidToc(URL url)
  4. isValidUrl(String input)
  5. isZipName(URI documentIRI, URLConnection connection)
  6. jarURLEntryResource(String jarURLString)
  7. lastModifiedURL(String urlstr)
  8. loadCookie(URL url)
  9. loadJar(String path, URL resource)