Java Unzip to Folder unzipFile(String zipFilePath, String targetPath)

Here you can find the source of unzipFile(String zipFilePath, String targetPath)

Description

unzip File

License

Open Source License

Declaration

public static void unzipFile(String zipFilePath, String targetPath) 

Method Source Code

//package com.java2s;
/*/* w w  w .  ja  v  a 2 s.co m*/
 * Copyright 2010-2011 ESunny.com All right reserved. This software is the confidential and proprietary information of
 * ESunny.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with ESunny.com.
 */

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;

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

public class Main {

    public static void unzipFile(String zipFilePath, String targetPath) {
        try {
            File zipFile = new File(zipFilePath);
            InputStream is = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                String zipPath = entry.getName();
                try {

                    if (entry.isDirectory()) {
                        File zipFolder = new File(targetPath + File.separator + zipPath);
                        if (!zipFolder.exists()) {
                            zipFolder.mkdirs();
                        }
                    } else {
                        File file = new File(targetPath + File.separator + zipPath);
                        if (!file.exists()) {
                            File pathDir = file.getParentFile();
                            pathDir.mkdirs();
                            file.createNewFile();
                        }

                        FileOutputStream fos = new FileOutputStream(file);
                        int bread;
                        while ((bread = zis.read()) != -1) {
                            fos.write(bread);
                        }
                        fos.close();

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            zis.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void unzipFile(byte[] content, String targetPath) {
        try {
            InputStream is = new ByteArrayInputStream(content);
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                String zipPath = entry.getName();
                try {

                    if (entry.isDirectory()) {
                        File zipFolder = new File(targetPath + File.separator + zipPath);
                        if (!zipFolder.exists()) {
                            zipFolder.mkdirs();
                        }
                    } else {
                        File file = new File(targetPath + File.separator + zipPath);
                        if (!file.exists()) {
                            File pathDir = file.getParentFile();
                            pathDir.mkdirs();
                            file.createNewFile();
                        }

                        FileOutputStream fos = new FileOutputStream(file);
                        int bread;
                        while ((bread = zis.read()) != -1) {
                            fos.write(bread);
                        }
                        fos.close();

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            zis.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

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