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

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

Description

unzip

License

Open Source License

Declaration

public static void unzip(String zipFile, String targetFolder, String... fileSuffixes) 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 {
    public static void unzip(String zipFile, String targetFolder, String... fileSuffixes) throws Exception {
        unzip(zipFile, targetFolder, true, fileSuffixes);
    }//  w  ww  .jav  a2 s .  c om

    /**
     * Unzip the component file to the user folder.
     * 
     * @param zipFile The component zip file
     * @param targetFolder The user folder
     * @param overwrite if overwrite existing files
     * @param fileSuffixes Case-insensitive Suffixes , if these parameter are set, only the files named with these
     * suffixes will be extracted
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static void unzip(String zipFile, String targetFolder, boolean overwrite, String... fileSuffixes)
            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());

                boolean exist = file.exists();
                if (entry.isDirectory()) {
                    if (!exist) {
                        file.mkdir();
                    }
                } else {
                    if (!overwrite && exist) {
                        continue;
                    }
                    if (fileSuffixes.length > 0 && !isReservedFile(file.getName(), fileSuffixes)) {
                        continue;
                    }
                    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;
            }
        }
    }

    private static boolean isReservedFile(String name, String[] fileSuffixes) {
        if (name != null) {
            String checkedName = name.toLowerCase();
            for (String element : fileSuffixes) {
                if (element.equals(checkedName) || checkedName.endsWith(element)) {
                    return true;
                }
            }
        }
        return false;
    }
}

Related

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