Java Unzip to Folder unzip(String zipFileName, String targetFolderPath)

Here you can find the source of unzip(String zipFileName, String targetFolderPath)

Description

unzip

License

Open Source License

Declaration

public static void unzip(String zipFileName, String targetFolderPath) 

Method Source Code


//package com.java2s;
/*/*from  www  . j  ava 2s. c om*/
* Copyright (c) 2010-2012 Research In Motion Limited. All rights reserved.
*
* This program and the accompanying materials are made available
* under  the terms of the Apache License, Version 2.0,
* which accompanies this distribution and is available at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* To use this code further you must also obtain a valid copy of
* InstallAnywhere 8.0 Enterprise/resource/IAClasses.zip
* Please visit http://www.flexerasoftware.com/products/installanywhere.htm for the terms.
* 
* Additionally, for the Windows(R) installer you must obtain a valid copy of vcredist_x86.exe
* Please visit http://www.microsoft.com/en-us/download/details.aspx?id=29 for the terms.
* 
*/

import java.io.BufferedOutputStream;
import java.io.File;
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 {
    public static void unzip(String zipFileName, String targetFolderPath) {
        Enumeration entries;
        ZipFile zipFile;

        try {
            // check target folder
            File targetFolder = new File(targetFolderPath);
            if (!targetFolder.exists()) {
                targetFolder.mkdirs();
            }
            String canonicalTargetPath = targetFolder.getCanonicalPath();

            // unzip zip file
            zipFile = new ZipFile(zipFileName);
            entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                if (entry.isDirectory()) {
                    // handle directory entry
                    File folder = new File(canonicalTargetPath + File.separator + entry.getName());
                    System.out.println(folder.getCanonicalPath());
                    if (!folder.exists() && !folder.mkdirs()) {
                        System.out.println("failed to create " + folder.getCanonicalPath());
                    }
                } else {
                    // handle file entry, we must ensure the parent folder exists.
                    System.out.println("Extracting file: " + entry.getName());
                    File file = new File(canonicalTargetPath + File.separator + entry.getName());
                    File parentFolder = file.getParentFile();
                    if (!parentFolder.exists() && !parentFolder.mkdirs()) {
                        System.out.println("failed to create parent folder " + parentFolder.getCanonicalPath());
                    }
                    copyFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file)));
                    file.setLastModified(entry.getTime());
                }
            }
            zipFile.close();
        } catch (IOException ex) {
            System.out.println("Unzip Exception: " + ex.getMessage());
        }
    }

    public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024 * 4];
        int len;

        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }
}

Related

  1. Unzip(String zipFile, String outputDirectory)
  2. unzip(String zipFile, String targetFolder)
  3. unzip(String zipFile, String targetFolder, String... fileSuffixes)
  4. unzip(String zipFile, String targetPath)
  5. unzip(String zipFileName, String outputDirectory)
  6. unzip(String zipFileName, String targetPath)
  7. unzip(String zipFileName, String unzipdir)
  8. unzipFile(String zipFile, File outputFolder)
  9. unzipFile(String zipFile, String extDir)