Java Path Extract nio extractZipArchive(final File zipFile, Path destDir)

Here you can find the source of extractZipArchive(final File zipFile, Path destDir)

Description

extract Zip Archive

License

Open Source License

Declaration

public static void extractZipArchive(final File zipFile, Path destDir)
            throws IOException, IllegalArgumentException 

Method Source Code


//package com.java2s;
/*/*from w  ww. ja  va 2s.c o m*/
 * Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, without warranties or
 * conditions of any kind, EITHER EXPRESS OR IMPLIED.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.io.File;

import java.io.IOException;

import java.nio.file.FileSystem;
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.logging.Logger;

public class Main {
    public static void extractZipArchive(final File zipFile, Path destDir)
            throws IOException, IllegalArgumentException {
        if (!destDir.toFile().exists() || !destDir.toFile().isDirectory()) {
            throw new IllegalArgumentException("can only unzip to directory");
        }

        if (!zipFile.exists()) {
            throw new IllegalArgumentException("zip file not found");
        }

        try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFile.toPath(), null)) {
            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 {
                    final Path destFile = Paths.get(destDir.toString(), file.toString());

                    Logger.getAnonymousLogger().info("Extracting file " + destFile);
                    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.getAnonymousLogger().info("Creating directory %s" + dirToCreate);
                        Files.createDirectory(dirToCreate);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
}

Related

  1. extractCurrentPath()
  2. extractFile(ZipInputStream zipIn, Path filePath)
  3. extractLanguageRelativePath(final Path absolutePath)
  4. extractLuaStacktrace(Path path, StackTraceElement[] stackTrace)
  5. extractVersion(Path pathToBinaries)
  6. unzipFileInArchive(FileChannel channel, String relpathInZip, File extractTo)