Java Unzip to Folder unzipToDirectory(File file, File dir)

Here you can find the source of unzipToDirectory(File file, File dir)

Description

unzip To Directory

License

Open Source License

Declaration

public static void unzipToDirectory(File file, File dir) throws ZipException, IOException 

Method Source Code

//package com.java2s;
/**/* ww  w.j a v  a 2  s  .  c om*/
 * This file is part of VisiCut.
 * Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
 * RWTH Aachen University - 52062 Aachen, Germany
 *
 *     VisiCut 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, or
 *     (at your option) any later version.
 *
 *     VisiCut 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 VisiCut.  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.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main {
    public static void unzipToDirectory(File file, File dir) throws ZipException, IOException {
        ZipFile zip = new ZipFile(file);
        Enumeration entries = zip.entries();
        File inputFile = null;
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String name = entry.getName();
            inputFile = new File(dir, name);
            File parent = inputFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            if (entry.isDirectory()) {
                inputFile.mkdir();
            } else {
                byte[] buf = new byte[1024];
                InputStream in = zip.getInputStream(entry);
                FileOutputStream out = new FileOutputStream(inputFile);
                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.close();
                in.close();
            }
        }
        zip.close();
    }
}

Related

  1. unzipFile(String zipFile, String outputFolder)
  2. unzipFile(String zipFileName, String outputDir)
  3. unzipFile(String zipFilePath, String releasePath)
  4. unzipFile(String zipFilePath, String targetPath)
  5. unzipFile(ZipFile zipFile, File destinationDir)
  6. unzipToFolder(final InputStream inputStream, final File outputFolder)
  7. unzipToFolder(final InputStream inputStream, final File outputFolder)
  8. unzipToFolder(String folderPath, InputStream source)
  9. unzipToFolder(ZipInputStream zipInputStream, File dirToExtractTo)