Java Unzip File unzip(File zipFile, File outputFolder)

Here you can find the source of unzip(File zipFile, File outputFolder)

Description

unzip

License

Apache License

Declaration

public static void unzip(File zipFile, File outputFolder) 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void unzip(File zipFile, File outputFolder) {

        byte[] buffer = new byte[1024];
        ZipInputStream zis = null;
        try {/*from  w  w w  . j  a v  a2  s.  c o m*/

            //get the zip file content
            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.getAbsolutePath() + File.separator + fileName);

                System.out.println("file unzip : " + newFile.getAbsoluteFile());

                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdir();
                if (!ze.isDirectory()) {
                    FileOutputStream fos = new FileOutputStream(newFile);

                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.flush();
                    fos.close();
                }
                ze = zis.getNextEntry();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                zis.closeEntry();
                zis.close();
            } catch (Exception e) {

            }
        }
    }
}

Related

  1. unzip(File zipFile, File destination)
  2. unzip(File zipFile, File destination, IProgressMonitor monitor)
  3. unzip(File zipfile, File directory)
  4. unzip(File zipfile, File directory)
  5. unzip(File zipFile, File outputDir)
  6. unZip(File zipFile, String desdir)
  7. unZip(File zipFile, String extPlace, boolean reservZipFile)
  8. unzip(File zipFileName, File targetDir)
  9. unzip(File zipName, File destDir)