Java Unzip File unzip(File zipFile, File destination, IProgressMonitor monitor)

Here you can find the source of unzip(File zipFile, File destination, IProgressMonitor monitor)

Description

Uzips the given zip into the specified destination

License

Open Source License

Parameter

Parameter Description
zipFile the file to unzip
destination the destination directory
monitor a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(File zipFile, File destination, IProgressMonitor monitor) throws IOException 

Method Source Code


//package com.java2s;
/*// ww w  .  j  ava 2s  . c  o m
 * Copyright (c) 2014, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html
 * 
 * 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 org.eclipse.core.runtime.IProgressMonitor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**
     * Uzips the given zip into the specified destination
     * 
     * @param zipFile the file to unzip
     * @param destination the destination directory
     * @param monitor
     * @throws IOException
     */
    public static void unzip(File zipFile, File destination, IProgressMonitor monitor) throws IOException {
        monitor.beginTask("Unzip " + zipFile.getName(), (int) zipFile.length());

        final int BUFFER_SIZE = 4096;

        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER_SIZE];

            File outFile = new File(destination, entry.getName());

            if (entry.isDirectory()) {
                if (!outFile.exists()) {
                    outFile.mkdirs();
                }
            } else {
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdirs();
                }

                OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));

                while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);

                    monitor.worked(count);
                }

                out.flush();
                out.close();
            }
        }

        zis.close();

        monitor.done();
    }
}

Related

  1. unzip(File zipFile, File destDir)
  2. unzip(File zipFile, File destDir)
  3. unzip(File zipFile, File destDir)
  4. unzip(File zipFile, File destDir)
  5. unzip(File zipFile, File destination)
  6. unzip(File zipfile, File directory)
  7. unzip(File zipfile, File directory)
  8. unzip(File zipFile, File outputDir)
  9. unzip(File zipFile, File outputFolder)