集册 Java实例教程 将文件存档提取到目标DIR目标DIR,并删除在JVM退出时提取的文件,如果标志DeleTeNeExchange是真的。

将文件存档提取到目标DIR目标DIR,并删除在JVM退出时提取的文件,如果标志DeleTeNeExchange是真的。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

669
如果标志deleteOnExit为true,则将文件存档提取到目标目录目标Dir,并删除在jvm退出时提取的文件。
/**时   代    Java - nowjava.com**/

//Licensed under the Apache License, Version 2.0 (the "License");

//package com.nowjava;

import java.io.File;


import java.io.IOException;

import java.net.URL;

import java.util.jar.JarEntry;

import java.util.jar.JarInputStream;


public class Main {

    public static void main(String[] argv) throws Exception {

        File archive = new File("Main.java");

        File targetDir = new File("Main.java");

        System.out.println(extract(archive, targetDir));

    }


    /**

     * The suffixes of the files to support.

     */

    public static final String[] SUPPORTED_FILES = new String[] { ".zip",

            ".jar", ".war" };//N o w J a v a . c o m 提 供


    public static boolean extract(File archive, File targetDir)

            throws IOException {

        return extract(archive.toURI().toURL(), targetDir, true);

    }


    public static boolean extract(File archive, File targetDir,

            boolean deleteOnExit) throws IOException {

        return extract(archive.toURI().toURL(), targetDir, deleteOnExit);

    }


    public static boolean extract(URL archive, File targetDir)

            throws IOException {

        return extract(archive, targetDir, true);

    }


    /**

     * Extracts the file {@code archive} to the target dir {@code targetDir} and deletes the 

     * files extracted upon jvm exit if the flag {@code deleteOnExit} is true.

     */

    public static boolean extract(URL archive, File targetDir,

            boolean deleteOnExit) throws IOException {

        String archiveStr = archive.toString();

        String jarEntry = null;

        int idx = archiveStr.indexOf("!/");

        if (idx != -1) {

            if (!archiveStr.startsWith("jar:")

                    && archiveStr.length() == idx + 2)

                return false;

            archive = new URL(archiveStr.substring(4, idx));

            jarEntry = archiveStr.substring(idx + 2);

        } else if (!isSupported(archiveStr))

            return false;


        JarInputStream jis = new JarInputStream(archive.openConnection()

                .getInputStream());

        if (!targetDir.exists())

            targetDir.mkdirs();

        JarEntry entry = null;

        while ((entry = jis.getNextJarEntry()) != null) {

            String entryName = entry.getName();

            File entryFile = new File(targetDir, entryName);

            if (!entry.isDirectory()) {

                if (jarEntry == null || entryName.startsWith(jarEntry)) {

                    if (!entryFile.exists()

                            || entryFile.lastModified() != entry.getTime())

                        extractEntry(entryFile, jis, entry, deleteOnExit);

                }

            }

        }

        try {

            jis.close();

        } catch (Exception e) {

        }

        return true;

    }


    /**

     * Returns true if the given {@code resource} is either a zip, jar or war file.

     */

    public static boolean isSupported(String resource) {

        int idx = resource.lastIndexOf('.');

        if (resource.length() == idx + 4) {

            for (int i = 0; i < SUPPORTED_FILES.length; i++) {

                
展开阅读全文