Java Unzip File unZip(File inFile, File unzipDir)

Here you can find the source of unZip(File inFile, File unzipDir)

Description

Given a File input it will unzip the file in a the unzip directory passed as the second parameter

License

Apache License

Parameter

Parameter Description
inFile The zip file as input
unzipDir The unzip directory where to unzip the zip file.

Exception

Parameter Description
IOException an exception

Declaration

public static void unZip(File inFile, File unzipDir) throws IOException 

Method Source Code

//package com.java2s;
/**//from  ww w .  ja  v  a  2s.  c  o 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.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    /**
     * Given a File input it will unzip the file in a the unzip directory
     * passed as the second parameter
     * @param inFile The zip file as input
     * @param unzipDir The unzip directory where to unzip the zip file.
     * @throws IOException
     */
    public static void unZip(File inFile, File unzipDir) throws IOException {
        Enumeration<? extends ZipEntry> entries;
        ZipFile zipFile = new ZipFile(inFile);

        try {
            entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    InputStream in = zipFile.getInputStream(entry);
                    try {
                        File file = new File(unzipDir, entry.getName());
                        if (!file.getParentFile().mkdirs()) {
                            if (!file.getParentFile().isDirectory()) {
                                throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                            }
                        }
                        OutputStream out = new FileOutputStream(file);
                        try {
                            byte[] buffer = new byte[8192];
                            int i;
                            while ((i = in.read(buffer)) != -1) {
                                out.write(buffer, 0, i);
                            }
                        } finally {
                            out.close();
                        }
                    } finally {
                        in.close();
                    }
                }
            }
        } finally {
            zipFile.close();
        }
    }
}

Related

  1. unzip(File file, File toDirectory)
  2. unzip(File file, String destination, String container)
  3. unZip(File file, String outPath, String zipFileName)
  4. unzip(File file, String targetDirectory)
  5. unzip(File fileToUnzip, File destinationDirectory)
  6. unzip(File input)
  7. unzip(File input, File outputDir)
  8. unzip(File input, File outputDirectory)
  9. unzip(File inputFile, File outputDir)