集册 Java实例教程 将文件解压缩到给定目录

将文件解压缩到给定目录

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

371
将文件解压缩到给定目录


import java.io.IOException;

import java.net.URI;

import java.nio.file.FileSystem;// 来 自 N o w J a v a . c o m - 时代Java

import java.nio.file.FileSystems;

import java.nio.file.FileVisitResult;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.StandardCopyOption;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.HashMap;

import java.util.Map;


public class Main{

    private static final StdOutLogger logger = StdOutLogger.getLogger();

    /**

     *

     * Unzips a file to a given directory

     *

     * @param zipFilename - the zip file to unzip

     * @param destDirname - the destination directory

     * @throws IOException

     */

    public static void unzip(String zipFilename, String destDirname)

            throws IOException {

        unzip(zipFilename, destDirname);

    }
    /*
    时代Java 提供
    */

    public static void unzip(String zipFilename, String destDirname,

            final UnzipProgressListener progressListener)

            throws IOException {


        final Path destDir = Paths.get(destDirname);

        //if the destination doesn't exist, create it

        if (Files.notExists(destDir)) {

            logger.debug(destDir + " does not exist. Creating...");

            Files.createDirectories(destDir);

        }


        try (FileSystem zipFileSystem = createZipFileSystem(zipFilename,

                false)) {

            final Path root = zipFileSystem.getPath("/");


            //walk the zip file tree and copy files to the destination

            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                @Override

                public FileVisitResult visitFile(Path file,

                        BasicFileAttributes attrs) throws IOException {

                    if (progressListener != null) {

                        progressListener.onBeginFileExtract(file.toString());


                    }

                    final Path destFile = Paths.get(destDir.toString(),

                            file.toString());

                    logger.debug("Extracting file " + file + " to "

                            + destFile + "\n");

                    Files.copy(file, destFile,

                            StandardCopyOption.REPLACE_EXISTING);

                    return FileVisitResult.CONTINUE;

                }


                @Override

                public FileVisitResult preVisitDirectory(Path dir,

                        BasicFileAttributes attrs) throws IOException {

                    final Path dirToCreate = Paths.get(destDir.toString(),

                            dir.toString());

                    if (Files.notExists(dirToCreate)) {

                        logger.debug("Creating directory " + dirToCreate

                                + "\n");

                        Files.createDirectory(dirToCreate);

                    }

                    return FileVisitResult.CONTINUE;

                }

            });

        }

    }

    /**

     * Returns a zip file system

     *

     * @param zipFilename to construct the file system from

     * @param create true if the zip file should be created

     * @return a zip file system

     * @throws IOException

     */

    private 
展开阅读全文