Java Unzip File unZip(File zip, File dest)

Here you can find the source of unZip(File zip, File dest)

Description

Dado un fichero comprimido en formato zip, lo descomprime en el directorio indicado.

License

Open Source License

Parameter

Parameter Description
zip el fichero a descomprimir
dest el directorio de destino

Exception

Parameter Description

Declaration

public static void unZip(File zip, File dest) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*//  w w w  .ja  va2  s  .c  o m
 * Copyright (C) 2016 jcgonzalez
 *
 * 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 3 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

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

    /**
     * Dado un fichero comprimido en formato zip, lo descomprime en el
     * directorio indicado.
     *
     * @param zip el fichero a descomprimir
     * @param dest el directorio de destino
     * @throws java.io.FileNotFoundException
     */
    public static void unZip(File zip, File dest) throws FileNotFoundException, IOException {
        FileInputStream fileInputStream = new FileInputStream(zip);
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
        ZipEntry entry = null;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            String outputFilename = dest.getAbsolutePath() + File.separator + entry.getName();

            int count;
            byte data[] = new byte[BUFFER_SIZE];

            FileOutputStream fos = new FileOutputStream(outputFilename);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos, BUFFER_SIZE);

            while ((count = zipInputStream.read(data, 0, BUFFER_SIZE)) != -1) {
                bufferedOutputStream.write(data, 0, count);
            }
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        }
        zipInputStream.close();
    }
}

Related

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