Java Unzip File unzip(File zipName, File destDir)

Here you can find the source of unzip(File zipName, File destDir)

Description

Convenience method unzipping zipName into destDir, cleaning up destDir first.

License

Apache License

Declaration

public static void unzip(File zipName, File destDir) throws IOException 

Method Source Code

//package com.java2s;
/*//from ww  w.  j a va  2  s  . co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.BufferedOutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Enumeration;

import java.util.LinkedHashSet;

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

public class Main {
    /** 
     * Convenience method unzipping zipName into destDir, cleaning up 
     * destDir first. 
     */
    public static void unzip(File zipName, File destDir) throws IOException {
        rm(destDir);
        destDir.mkdir();

        ZipFile zipFile = new ZipFile(zipName);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

            InputStream in = zipFile.getInputStream(entry);
            File targetFile = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                // allow unzipping with directory structure
                targetFile.mkdirs();
            } else {
                if (targetFile.getParentFile() != null) {
                    // be on the safe side: do not rely on that directories are always extracted
                    // before their children (although this makes sense, but is it guaranteed?)
                    targetFile.getParentFile().mkdirs();
                }
                OutputStream out = new BufferedOutputStream(
                        new FileOutputStream(targetFile));

                byte[] buffer = new byte[8192];
                int len;
                while ((len = in.read(buffer)) >= 0) {
                    out.write(buffer, 0, len);
                }

                in.close();
                out.close();
            }
        }

        zipFile.close();
    }

    /**
     * Deletes one or more files or directories (and everything underneath it).
     * 
     * @throws IOException if any of the given files (or their subhierarchy files in case
     * of directories) cannot be removed.
     */
    public static void rm(File... locations) throws IOException {
        LinkedHashSet<File> unremoved = rm(new LinkedHashSet<File>(),
                locations);
        if (!unremoved.isEmpty()) {
            StringBuilder b = new StringBuilder(
                    "Could not remove the following files (in the order of attempts):\n");
            for (File f : unremoved) {
                b.append("   ").append(f.getAbsolutePath()).append("\n");
            }
            throw new IOException(b.toString());
        }
    }

    private static LinkedHashSet<File> rm(LinkedHashSet<File> unremoved,
            File... locations) {
        if (locations != null) {
            for (File location : locations) {
                if (location != null && location.exists()) {
                    if (location.isDirectory()) {
                        rm(unremoved, location.listFiles());
                    }

                    if (!location.delete()) {
                        unremoved.add(location);
                    }
                }
            }
        }
        return unremoved;
    }
}

Related

  1. unzip(File zipFile, File outputDir)
  2. unzip(File zipFile, File outputFolder)
  3. unZip(File zipFile, String desdir)
  4. unZip(File zipFile, String extPlace, boolean reservZipFile)
  5. unzip(File zipFileName, File targetDir)
  6. unZip(File zipPath, File destPath)
  7. unzip(File zippedFile)
  8. unzip(File zippedFile, File outputDirectory)
  9. unzip(File zippedFile, File targetDir)