Sitemap

Thursday, June 25, 2015

Java: Extract a JAR file

import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class UnzipJar {

    private static final String JAR_PATH = "D:\\test\\Java_gpl_v1.04.jar";

    public static void unzipJarFile(Path jar) throws IOException {
        if (!Files.exists(jar))
            return;

        String fnJar = jar.getFileName().toString();
        String fn = fnJar.substring(0, fnJar.lastIndexOf(".jar"));
        System.out.println(fnJar + " " + fn);
        Path dst = jar.getParent().resolve(fn);
        Files.createDirectory(dst);
        JarFile jf = new JarFile(jar.toString());

        //create directory
        for (Enumeration<JarEntry> enums = jf.entries(); enums.hasMoreElements();) {
            JarEntry entry = enums.nextElement();
            if (entry.isDirectory()) {
                Files.createDirectories(dst.resolve(entry.getName()));
            }
        }
        //copy file
        for (Enumeration<JarEntry> enums = jf.entries(); enums.hasMoreElements();) {
            JarEntry entry = enums.nextElement();
            if (!entry.isDirectory()) {
                Files.copy(jf.getInputStream(entry), dst.resolve(entry.getName()), StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        unzipJarFile(Paths.get(JAR_PATH));
    }
}


No comments:

Post a Comment