Java Unzip to Folder unZipFile(String zipFile, String outputFolder)

Here you can find the source of unZipFile(String zipFile, String outputFolder)

Description

un Zip File

License

Open Source License

Declaration

public static void unZipFile(String zipFile, String outputFolder) 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

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

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

public class Main {
    public static void unZipFile(String zipFile, String outputFolder) {
        byte[] buffer = new byte[1024];

        try {/*from   w  w w .  j a v  a  2  s . c  o  m*/
            //create output directory is not exists
            File folder = new File(outputFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }

            //get the zip file content
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);

                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. unzip(String zipFileName, String targetFolderPath)
  2. unzip(String zipFileName, String targetPath)
  3. unzip(String zipFileName, String unzipdir)
  4. unzipFile(String zipFile, File outputFolder)
  5. unzipFile(String zipFile, String extDir)
  6. unzipFile(String zipFile, String outputFolder)
  7. unzipFile(String zipFileName, String outputDir)
  8. unzipFile(String zipFilePath, String releasePath)
  9. unzipFile(String zipFilePath, String targetPath)