Java Unzip File decompresses(File file, File destinationDirectory)

Here you can find the source of decompresses(File file, File destinationDirectory)

Description

Descompress a file zip.

License

Open Source License

Parameter

Parameter Description
file - the file compress
destinationDirectory - Destination descompress directory

Exception

Parameter Description
IOException an exception
ZipException an exception

Return

the descompress files

Declaration

public static File[] decompresses(File file, File destinationDirectory) throws ZipException, IOException 

Method Source Code


//package com.java2s;
/*//  w  w  w. jav  a  2 s.  co m
 *     @(#)ZipUtils.java   0.6 10/11/22
 * 
 *   Copyright (c) 2010 Felipe Priuli
 *
 *   This file is part of OpenSutils-Br4J.
 *
 *   OpenSutils-Br4J is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, any later version.
 *
 *   OpenSutils-Br4J 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main {
    /**
     * Descompress a file zip.
     * @param    file - the file compress
     * @param    destinationDirectory - Destination descompress directory
     * @return   the descompress files
     * @throws    IOException
     * @throws    ZipException 
     */
    public static File[] decompresses(File file, File destinationDirectory) throws ZipException, IOException {

        if (!destinationDirectory.exists()) {
            destinationDirectory.mkdir();
        }
        if (!destinationDirectory.isDirectory()) {
            throw new IllegalArgumentException("Second param not is a Directory");
        }
        ZipFile arquivoZip = new ZipFile(file);
        File[] retorno = new File[arquivoZip.size()];
        Enumeration<? extends ZipEntry> enumerador = arquivoZip.entries();
        int countFiles = 0;
        while (enumerador.hasMoreElements()) {
            ZipEntry arquivoDentroZip = enumerador.nextElement();

            //Criando estrutura de pastas
            if (arquivoDentroZip.isDirectory()) {
                (new File(arquivoDentroZip.getName())).mkdir();
                continue;
            }

            //arquivos dentro do zip
            InputStream arquivoLido = null;
            OutputStream outputStream = null;
            BufferedOutputStream bufferArquivo = null;
            File outFile = new File(
                    destinationDirectory.getAbsolutePath() + File.separator + arquivoDentroZip.getName());
            try {
                arquivoLido = arquivoZip.getInputStream(arquivoDentroZip);
                outputStream = new FileOutputStream(outFile);
                bufferArquivo = new BufferedOutputStream(outputStream);

                byte[] buffer;
                int numeroBytes;
                buffer = new byte[1024];
                while ((numeroBytes = arquivoLido.read(buffer)) >= 0)
                    bufferArquivo.write(buffer, 0, numeroBytes);

                bufferArquivo.flush();
                outputStream.flush();

                retorno[countFiles++] = outFile;
            } finally {
                if (arquivoLido != null)
                    arquivoLido.close();
                if (bufferArquivo != null)
                    bufferArquivo.close();
                if (outputStream != null)
                    outputStream.close();
            }

        }

        return retorno;
    }
}

Related

  1. decompress(String fileName, String destinationFolder)
  2. decompress(String filepath, File outpathdir, String includes)
  3. decompress(String zipFile, String targetPath)
  4. decompressFile(File destFile, ZipInputStream zis)
  5. decompressFile(final File toDecompress, final File destinationFile)
  6. decompressFile(String inName)
  7. decompressGzip(File src, File trgt)