Java Unzip File unzip(File zippedFile, File targetDir)

Here you can find the source of unzip(File zippedFile, File targetDir)

Description

unzip

License

Apache License

Declaration

@SuppressWarnings("ResultOfMethodCallIgnored")
    public static void unzip(File zippedFile, File targetDir)
            throws ZipException, IOException, FileNotFoundException 

Method Source Code

//package com.java2s;
/**//from   w w  w .  ja va2  s  .c om
 * Copyright (C) 2012 NICE Systems ltd.
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.
 *
 * @author Igor Cher
 * @version %I%, %G%
 */

import java.io.*;

import java.util.Enumeration;

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

public class Main {
    private static final int BUFFER_SIZE = 4096;

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static void unzip(File zippedFile, File targetDir)
            throws ZipException, IOException, FileNotFoundException {
        targetDir.mkdirs();

        ZipFile zipFile = new ZipFile(zippedFile);

        try {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File targetFile = new File(targetDir, entry.getName());
                if (entry.isDirectory()) {
                    targetFile.mkdirs();
                } else {
                    InputStream input = zipFile.getInputStream(entry);
                    try {
                        OutputStream output = new FileOutputStream(targetFile);
                        try {
                            copy(input, output);
                        } finally {
                            output.close();
                        }
                    } finally {
                        input.close();
                    }
                }
            }
        } finally {
            zipFile.close();
        }
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];

        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
    }
}

Related

  1. unzip(File zipFileName, File targetDir)
  2. unzip(File zipName, File destDir)
  3. unZip(File zipPath, File destPath)
  4. unzip(File zippedFile)
  5. unzip(File zippedFile, File outputDirectory)
  6. unzip(final byte[] zippedContent)
  7. unzip(final File dir, final ZipInputStream from)
  8. unzip(final File zip, final File dir)
  9. unzip(final File zipFile, final File destDir)