Java Unzip File unzip(File zip, File directory)

Here you can find the source of unzip(File zip, File directory)

Description

unzip

License

Apache License

Declaration

public static void unzip(File zip, File directory) throws ZipException, IOException 

Method Source Code


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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;

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

public class Main {
    public static void unzip(File zip, File directory) throws ZipException, IOException {
        ZipFile zipFile = new ZipFile(zip);
        try {//from w ww. j a  v  a 2 s. com
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = entries.nextElement();
                if (zipEntry.isDirectory()) {
                    File temp = new File(directory + File.separator + zipEntry.getName());
                    temp.mkdirs();
                    continue;
                }
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
                File f = new File(directory + File.separator + zipEntry.getName());
                File f_p = f.getParentFile();
                if (f_p != null && !f_p.exists()) {
                    f_p.mkdirs();
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
                int len = -1;
                byte[] bs = new byte[2048];
                while ((len = bis.read(bs, 0, 2048)) != -1) {
                    bos.write(bs, 0, len);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
        } finally {
            zipFile.close();
        }
    }
}

Related

  1. unzip(File targetZip, File dirToExtract)
  2. unzip(File zip)
  3. unZip(File zip, File dest)
  4. unzip(File zip, File destination)
  5. unzip(File zip, File dir)
  6. unzip(File zip, File extractTo)
  7. unzip(File zip, File extractTo)
  8. unzip(File zip, File location)
  9. unzip(File zip, File targetDir)