Java Unzip File unzip(File fileToUnzip, File destinationDirectory)

Here you can find the source of unzip(File fileToUnzip, File destinationDirectory)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File fileToUnzip, File destinationDirectory) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) JavaPEG developers//www  . j  a  v a  2s. c  om
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

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

public class Main {
    public static void unzip(File fileToUnzip, File destinationDirectory) throws IOException {

        //create output directory is not exists
        if (!destinationDirectory.exists()) {
            destinationDirectory.mkdirs();
        } else {
            if (destinationDirectory.isFile()) {
                destinationDirectory = destinationDirectory.getParentFile();
            }
        }

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(fileToUnzip))) {

            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {

                File newFile = new File(destinationDirectory, ze.getName());

                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                newFile.getParentFile().mkdirs();

                try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile))) {

                    byte[] buffer = new byte[2048];
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        bos.write(buffer, 0, len);
                    }
                }

                zis.closeEntry();
                ze = zis.getNextEntry();
            }
        }
    }
}

Related

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