Java Unzip File unzip(File zip, File targetDir)

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

Description

unzip

License

Open Source License

Declaration

public static void unzip(File zip, File targetDir) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

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

public class Main {
    public static void unzip(File zip, File targetDir) throws IOException {
        unzip(new FileInputStream(zip), targetDir);
    }/*  ww  w .  j  a  v  a 2  s.  c  o  m*/

    public static void unzip(InputStream in, File targetDir) throws IOException {
        ZipInputStream zipIn = null;
        try {
            zipIn = new ZipInputStream(in);
            byte[] b = new byte[8192];
            ZipEntry zipEntry;
            while ((zipEntry = zipIn.getNextEntry()) != null) {
                File file = new File(targetDir, zipEntry.getName());
                if (!zipEntry.isDirectory()) {
                    File parent = file.getParentFile();
                    if (!parent.exists()) {
                        parent.mkdirs();
                    }
                    FileOutputStream fos = new FileOutputStream(file);
                    try {
                        int r;
                        while ((r = zipIn.read(b)) != -1) {
                            fos.write(b, 0, r);
                        }
                    } finally {
                        fos.close();
                    }
                } else {
                    file.mkdirs();
                }
                zipIn.closeEntry();
            }
        } finally {
            if (zipIn != null) {
                zipIn.close();
            }
            in.close();
        }
    }
}

Related

  1. unzip(File zip, File dir)
  2. unzip(File zip, File directory)
  3. unzip(File zip, File extractTo)
  4. unzip(File zip, File extractTo)
  5. unzip(File zip, File location)
  6. unzip(File zip, File toDir)
  7. unzip(File zip, File toDir)
  8. unzip(File zip, String folder, File into)
  9. unZip(File zipf, String targetDir)