Java Unzip File unzip(final InputStream zipFile, final File targetFolder)

Here you can find the source of unzip(final InputStream zipFile, final File targetFolder)

Description

Unzips a given ZIP file InputStream into a given target File folder .

License

Apache License

Parameter

Parameter Description
zipFile the ZIP file to unzip as InputStream
targetFolder the target File folder where to unzip the files

Exception

Parameter Description
IOException if any internal file operation fails
IllegalArgumentException if the given target File folder is not a directory

Declaration

public static void unzip(final InputStream zipFile, final File targetFolder)
        throws IOException, IllegalArgumentException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class Main {
    /**//  w  w w.  j ava2 s . co m
     * Unzips a given ZIP file {@link InputStream} into a given target
     * {@link File folder}.
     * <p>
     * The given target {@link File folder} must either exist (and must really be
     * a directory) or is must be producible by the calling application.
     * 
     * @param zipFile
     *           the ZIP file to unzip as {@link InputStream}
     * @param targetFolder
     *           the target {@link File folder} where to unzip the files
     * @throws IOException
     *            if any internal file operation fails
     * @throws IllegalArgumentException
     *            if the given target {@link File folder} is not a directory
     */
    public static void unzip(final InputStream zipFile, final File targetFolder)
            throws IOException, IllegalArgumentException {
        if (targetFolder.exists()) {
            if (!targetFolder.isDirectory()) {
                throw new IllegalArgumentException(
                        String.format("Given existing target folder '%1$s' is not a directory!", targetFolder));
            }
        } else {
            if (!targetFolder.mkdirs()) {
                throw new IOException(String.format("Could not create target folder '%1$s'!", targetFolder));
            }
        }

        final ZipInputStream zis = new ZipInputStream(zipFile);
        final byte[] buffer = new byte[8192];

        try {
            for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
                try {
                    if (entry.isDirectory()) {
                        new File(targetFolder, entry.getName()).mkdirs();
                    } else {
                        final File targetFile = new File(targetFolder, entry.getName());
                        targetFile.getParentFile().mkdirs();

                        final FileOutputStream fos = new FileOutputStream(targetFile);
                        try {
                            for (int len = zis.read(buffer); len > 0; len = zis.read(buffer)) {
                                fos.write(buffer, 0, len);
                            }
                        } finally {
                            fos.close();
                        }
                    }
                } finally {
                    zis.closeEntry();
                }
            }
        } finally {
            zis.close();
        }
    }

    /**
     * Unzips a given ZIP {@link File} into a given target {@link File folder}.
     * <p>
     * The given target {@link File folder} must either exist (and must really be
     * a directory) or is must be producible by the calling application.
     * 
     * @param zipFile
     *           the ZIP {@link File} to unzip
     * @param targetFolder
     *           the target {@link File folder} where to unzip the files
     * @throws IOException
     *            if any internal file operation fails
     * @throws IllegalArgumentException
     *            if the given ZIP {@link File} is not valid or the given target
     *            {@link File folder} is not a directory
     */
    public static void unzip(final File zipFile, final File targetFolder)
            throws IOException, IllegalArgumentException {
        if (zipFile == null || !zipFile.exists() || !zipFile.isFile()) {
            throw new IllegalArgumentException(
                    String.format("Given ZIP file '%1$s' is null, does not exist or is not a file!", zipFile));
        }

        unzip(new FileInputStream(zipFile), targetFolder);
    }
}

Related

  1. unzip(final File dir, final ZipInputStream from)
  2. unzip(final File zip, final File dir)
  3. unzip(final File zipFile, final File destDir)
  4. unzip(final File zipFile, final String targetDir)
  5. unzip(final InputStream is, final File outputDirectory)
  6. unZip(final String result)
  7. unzip(final String zipFile, final String outputFolder, final boolean createFolder)