Java URL Connection createWebStartDirectory(String name, String jarUrl)

Here you can find the source of createWebStartDirectory(String name, String jarUrl)

Description

create Web Start Directory

License

EUPL

Declaration

protected static String createWebStartDirectory(String name, String jarUrl) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  ww  w. j ava2s .c o  m*/
 * Copyright 2004-2014 SmartBear Software
 *
 * Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://ec.europa.eu/idabc/eupl
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the Licence for the specific language governing permissions and limitations
 * under the Licence.
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    protected static String createWebStartDirectory(String name, String jarUrl) throws IOException {

        String deploymentUserTmp = System.getProperty("deployment.user.tmp");
        JarFile jar = getJar(jarUrl);
        String dir = createDirectory(deploymentUserTmp, name);
        extract(jar, dir);
        return dir;
    }

    private static JarFile getJar(String jarUrl) throws MalformedURLException, IOException {
        // String reportsJarUrl = System.getProperty("reports.jar.url");
        URL url = new URL("jar:" + jarUrl + "!/");
        JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
        JarFile jar = jarConnection.getJarFile();
        return jar;
    }

    private static String createDirectory(String deploymentUserTmp, String folderName) {
        File folder = new File(deploymentUserTmp + File.separator + folderName);
        folder.mkdir();
        // System.out.println(folder.getAbsolutePath());
        return folder.getAbsolutePath();
    }

    private static void extract(JarFile jar, String dir) throws IOException, FileNotFoundException {
        makeDirectories(jar, dir);
        extractFiles(jar, dir);
    }

    @SuppressWarnings("unchecked")
    private static void makeDirectories(JarFile jar, String eviwareDir) {
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry file = (JarEntry) entries.nextElement();
            File f = new File(eviwareDir + File.separator + file.getName());
            if (file.isDirectory()) { // if its a directory, create it
                f.mkdir();
                // System.out.println(f);
            }
        }
    }

    @SuppressWarnings("unchecked")
    private static void extractFiles(JarFile jar, String eviwareDir) throws IOException, FileNotFoundException {
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry file = (JarEntry) entries.nextElement();
            File f = new File(eviwareDir + File.separator + file.getName());

            if (file.isDirectory()) { // if its a directory, skip it
                continue;
            }
            // System.out.println(f);
            InputStream is = jar.getInputStream(file);
            FileOutputStream fos = new FileOutputStream(f);

            while (is.available() > 0) {
                fos.write(is.read());
            }

            fos.close();
            is.close();
        }
    }
}

Related

  1. byteArrayToURL(final byte[] bytes)
  2. closeURLClassLoader(URLClassLoader clazzLdr)
  3. createImgUrlConnection(String url)
  4. createURLClassLoader(String dirPath)
  5. createURLConnection(String url)
  6. doGet(String targetURL)
  7. doGet(String url, String charset)
  8. doPost(final URL url, final Map parameters, final boolean encode)
  9. doPost(String url, String params, String charset)