Java Unzip to Folder unzip(String zipFile, String targetFolder)

Here you can find the source of unzip(String zipFile, String targetFolder)

Description

Unzip the component file to the user folder.

License

Open Source License

Parameter

Parameter Description
zipFile The component zip file
targetFolder The user folder

Exception

Parameter Description
Exception an exception

Declaration

public static void unzip(String zipFile, String targetFolder) throws Exception 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;

import java.io.FileOutputStream;

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
     * Unzip the component file to the user folder.
     * 
     * @param zipFile The component zip file
     * @param targetFolder The user folder
     * @return
     * @throws Exception
     */
    public static void unzip(String zipFile, String targetFolder) throws Exception {
        Exception exception = null;
        ZipFile zip = new ZipFile(zipFile);
        byte[] buf = new byte[8192];

        try {
            Enumeration<ZipEntry> enumeration = (Enumeration<ZipEntry>) zip.entries();
            while (enumeration.hasMoreElements()) {
                ZipEntry entry = enumeration.nextElement();

                File file = new File(targetFolder, entry.getName());

                if (entry.isDirectory()) {
                    if (!file.exists()) {
                        file.mkdir();
                    }
                } else {

                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }

                    InputStream zin = zip.getInputStream(entry);
                    OutputStream fout = new FileOutputStream(file);
                    // check if parent folder exists
                    File dir = file.getParentFile();
                    if (dir.isDirectory() && !dir.exists()) {
                        dir.mkdirs();
                    }

                    try {
                        while (true) {
                            int bytesRead = zin.read(buf);
                            if (bytesRead == -1) { // end of file
                                break;
                            }
                            fout.write(buf, 0, bytesRead);

                        }
                        fout.flush();
                    } catch (Exception e) {
                        exception = e;
                        // stop looping
                        return;
                    } finally {
                        zin.close();
                        fout.close();
                    }
                }
            }
        } catch (Exception e) {
            exception = e;
        } finally {
            zip.close();

            if (exception != null) {
                // notify caller with exception
                throw exception;
            }
        }
    }
}

Related

  1. unzip(String zip, String dest)
  2. unzip(String zip, String folder)
  3. unzip(String zip, String path)
  4. unzip(String zip, String unzipDir, int bufferSize)
  5. Unzip(String zipFile, String outputDirectory)
  6. unzip(String zipFile, String targetFolder, String... fileSuffixes)
  7. unzip(String zipFile, String targetPath)
  8. unzip(String zipFileName, String outputDirectory)
  9. unzip(String zipFileName, String targetFolderPath)