Java Unzip to Folder unzipFile(ZipFile zipFile, File destinationDir)

Here you can find the source of unzipFile(ZipFile zipFile, File destinationDir)

Description

Extracts content of the zipFile to given destination directory

License

Open Source License

Parameter

Parameter Description
zipFile the file to unzip
destinationDir destination directory for the content of the zipFile

Exception

Parameter Description
FileNotFoundException if a file could not be created
IOException if IO error occurs

Declaration

public static void unzipFile(ZipFile zipFile, File destinationDir)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileNotFoundException;
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.ZipFile;

public class Main {
    /**//from  ww w  .  j  av  a 2 s .  c o m
     * Extracts content of the zipFile to given destination directory
     * 
     * @param zipFile
     *            the file to unzip
     * @param destinationDir
     *            destination directory for the content of the zipFile
     * @throws FileNotFoundException
     *             if a file could not be created
     * @throws IOException
     *             if IO error occurs
     */
    public static void unzipFile(ZipFile zipFile, File destinationDir)
            throws FileNotFoundException, IOException {

        if (!destinationDir.exists()) {
            destinationDir.mkdirs();
        }

        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {

                File file = new File(destinationDir, entry.getName());
                File parentFile = file.getParentFile();
                if (parentFile != null) {
                    parentFile.mkdirs();
                }
                InputStream inputStream = zipFile.getInputStream(entry);
                FileOutputStream outputStream = new FileOutputStream(file);

                try {
                    copyStream(inputStream, outputStream);
                } finally {
                    inputStream.close();
                    outputStream.close();
                }
            }
        }
    }

    /**
     * Perform copy from input stream to output stream.
     * 
     * @param is
     *            source stream
     * @param os
     *            result stream
     * @throws IOException
     */
    public static void copyStream(InputStream is, OutputStream os)
            throws IOException {
        byte[] buffer = new byte[131072];
        int bytesRead;

        while ((bytesRead = is.read(buffer)) > 0) {
            os.write(buffer, 0, bytesRead);
        }
    }
}

Related

  1. unZipFile(String zipFile, String outputFolder)
  2. unzipFile(String zipFile, String outputFolder)
  3. unzipFile(String zipFileName, String outputDir)
  4. unzipFile(String zipFilePath, String releasePath)
  5. unzipFile(String zipFilePath, String targetPath)
  6. unzipToDirectory(File file, File dir)
  7. unzipToFolder(final InputStream inputStream, final File outputFolder)
  8. unzipToFolder(final InputStream inputStream, final File outputFolder)
  9. unzipToFolder(String folderPath, InputStream source)