Java Unzip File unzip(File zip, File destination)

Here you can find the source of unzip(File zip, File destination)

Description

Unzips the given zip File into a new directory called destination.

License

Open Source License

Parameter

Parameter Description
zip The zip File to unzip.
destination The directory where to put the contents of the zip File . It may be created if it doesn't exist.

Exception

Parameter Description
IOException Something goes wrong while executing some file operations.

Declaration

public static void unzip(File zip, File destination) throws IOException 

Method Source Code


//package com.java2s;
/*//from w ww  . ja v  a2  s.  c o m
 * This file is part of QuarterBukkit-Plugin.
 * Copyright (c) 2012 QuarterCode <http://www.quartercode.com/>
 *
 * QuarterBukkit-Plugin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QuarterBukkit-Plugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QuarterBukkit-Plugin. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

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

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

public class Main {
    /**
     * Unzips the given zip {@link File} into a new directory called destination.
     * 
     * @param zip The zip {@link File} to unzip.
     * @param destination The directory where to put the contents of the zip {@link File}. It may be created if it doesn't exist.
     * @throws IOException Something goes wrong while executing some file operations.
     */
    public static void unzip(File zip, File destination) throws IOException {

        ZipFile zipFile = null;

        try {
            zipFile = new ZipFile(zip);

            for (ZipEntry zipEntry : Collections.list(zipFile.entries())) {
                File file = new File(destination, zipEntry.getName());

                if (zipEntry.isDirectory()) {
                    file.mkdirs();
                } else {
                    new File(file.getParent()).mkdirs();

                    InputStream inputStream = null;
                    OutputStream outputStream = null;
                    try {
                        inputStream = zipFile.getInputStream(zipEntry);
                        outputStream = new FileOutputStream(file);

                        byte[] buffer = new byte[0xFFFF];
                        for (int lenght; (lenght = inputStream.read(buffer)) != -1;) {
                            outputStream.write(buffer, 0, lenght);
                        }
                    } catch (IOException e) {
                        throw e;
                    } finally {
                        if (outputStream != null) {
                            try {
                                outputStream.close();
                            } catch (IOException e) {
                                // Ignore
                            }
                        }
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                // Ignore
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
    }
}

Related

  1. unzip(File tarFile, File destinationDir)
  2. unzip(File target)
  3. unzip(File targetZip, File dirToExtract)
  4. unzip(File zip)
  5. unZip(File zip, File dest)
  6. unzip(File zip, File dir)
  7. unzip(File zip, File directory)
  8. unzip(File zip, File extractTo)
  9. unzip(File zip, File extractTo)