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

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

Description

unzip File

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static void unzipFile(String zipFilePath, String releasePath) throws IOException 

Method Source Code

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

import java.io.File;

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

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    @SuppressWarnings("unchecked")
    public static void unzipFile(String zipFilePath, String releasePath) throws IOException {
        ZipFile zipFile = new ZipFile(zipFilePath);
        Enumeration<ZipEntry> enumeration = (Enumeration<ZipEntry>) zipFile.entries();
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        ZipEntry zipEntry = null;
        String zipEntryNameStr = "";
        String[] zipEntryNameArray = null;
        while (enumeration.hasMoreElements()) {
            zipEntry = enumeration.nextElement();
            zipEntryNameStr = zipEntry.getName();
            zipEntryNameArray = zipEntryNameStr.split("/");
            String path = releasePath;
            File root = new File(releasePath);
            if (!root.exists()) {
                root.mkdir();// www  .  j ava  2  s . c o  m
            }
            for (int i = 0; i < zipEntryNameArray.length; i++) {
                if (i < zipEntryNameArray.length - 1) {
                    path = path + File.separator + zipEntryNameArray[i];
                    new File(path).mkdir();
                } else {
                    if (zipEntryNameStr.endsWith(File.separator)) {
                        new File(releasePath + zipEntryNameStr).mkdir();
                    } else {
                        inputStream = zipFile.getInputStream(zipEntry);
                        fileOutputStream = new FileOutputStream(new File(releasePath + zipEntryNameStr));
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = inputStream.read(buf)) > 0) {
                            fileOutputStream.write(buf, 0, len);
                        }
                        inputStream.close();
                        fileOutputStream.close();
                    }
                }
            }
        }
        zipFile.close();
    }
}

Related

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