Java Path File Name nio unzipFile(String fileName, String targetPath)

Here you can find the source of unzipFile(String fileName, String targetPath)

Description

Unzips a zip.

License

Apache License

Parameter

Parameter Description
fileName a parameter
targetDirName a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzipFile(String fileName, String targetPath) throws IOException 

Method Source Code

//package com.java2s;
/**/* w w w .  ja va 2  s .  c o  m*/
 * Copyright 2011-2013 BBe Consulting GmbH
 *
 * 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.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import java.nio.channels.Channels;
import java.nio.channels.FileChannel;

public class Main {
    /**
     * Unzips a zip.
     * 
     * @param fileName
     * @param targetDirName
     * @throws IOException
     */
    public static void unzipFile(String fileName, String targetPath) throws IOException {

        final File targetDir = new File(targetPath);
        final ZipFile sourceZip = new ZipFile(fileName);

        @SuppressWarnings("unchecked")
        final Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) sourceZip.entries();

        while (entries.hasMoreElements()) {
            ZipEntry currentEntry = entries.nextElement();
            File targetFile = new File(targetDir, currentEntry.getName());
            // create sub directories if needed
            targetFile.getParentFile().mkdirs();
            // write file if it's not a directory
            if (!currentEntry.isDirectory()) {
                writeFileFromZip(targetFile, currentEntry, sourceZip);
            }
        }
    }

    private static void writeFileFromZip(File targetFile, ZipEntry zipEntry, ZipFile archive) throws IOException {

        FileChannel output = null;
        FileOutputStream rawOut = null;
        try {
            final InputStream rawIn = archive.getInputStream(zipEntry);
            rawOut = new FileOutputStream(targetFile);
            output = rawOut.getChannel();
            output.transferFrom(Channels.newChannel(rawIn), 0, zipEntry.getSize());
        } finally {
            if (output != null) {
                output.close();
            }
            if (rawOut != null) {
                rawOut.close();
            }
        }
    }
}

Related

  1. shortenFileName(Path file, List dirs)
  2. storeFile(String fileName, InputStream inputStream, Path targetPath)
  3. subDirectoryNames(final Path dir)
  4. toClassName(String path)
  5. toResourcePath(String fileName)
  6. uploadFileToServer(InputStream input, String filename, String usercode, String basePath)
  7. validateDockerfilePath(String name)
  8. which(Path path, String name)
  9. writeDataFile(final String resourceName, final Path sourcePath)