public static String createIndex(File dir, String path) throws IOException {
        if (!path.endsWith("/"))
            path += "/";
        // calculate name column width
        int w = 21; // minimum width
        for (String name : dir.list())
            if (name.length() > w)
                w = name.length();
        w += 2; // with room for added slash and space
        // note: we use apache's format, for consistent user experience
        Formatter f = new Formatter(Locale.US);
        f.format("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">%n" +
            "<html><head><title>Index of %s</title></head>%n" +
            "<body><h1>Index of %s</h1>%n" +
            "<pre> Name%" + (w - 5) + "s Last modified      Size<hr>",
            path, path, "");
        if (path.length() > 1) // add parent link if not root path
            f.format(" <a href=\"%s/\">Parent Directory</a>%"
                + (w + 5) + "s-%n", getParentPath(path), "");
        for (File file : dir.listFiles()) {
            try {
                String name = file.getName() + (file.isDirectory() ? "/" : "");
                String size = file.isDirectory() ? "- "
                                                 : toSizeString(file.length());
                // properly url-encode the link
                String link = new URI(null, path + name, null).toASCIIString(); 
                f.format(" <a href=\"%s\">%s</a>%-" + (w - name.length()) +
                    "s&#8206;%td-%<tb-%<tY %<tR%6s%n",
                    link, name, "", file.lastModified(), size);
            } catch (URISyntaxException ignore) {}
        }
        f.format("</pre></body></html>");
        return f.toString();
    }