Java Unzip File unzip(File zipFile, File destDir)

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

Description

Unzip a zip file to a destination.

License

Open Source License

Parameter

Parameter Description
zipFile a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(File zipFile, File destDir) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 {
    /**/*  www  .  j  a  v a 2 s.c om*/
     * Unzip a zip file to a destination.
     * The destination must exist before calling this method!
     * @param zipFile
     * @throws IOException 
     */
    public static void unzip(File zipFile, File destDir) throws IOException {
        unzip(zipFile, destDir, new byte[1024 * 32]);
    }

    public static void unzip(File zipFile, File destDir, byte[] buffer) throws IOException {
        if (!destDir.exists()) {
            throw new IOException("Destination directory " + destDir.getAbsolutePath() + " doesn't exist");
        }

        ZipInputStream zis = null;
        FileOutputStream fos = null;
        try {
            zis = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry entry = zis.getNextEntry();

            while (entry != null) {
                File extractTo = new File(destDir, entry.getName());
                if (entry.isDirectory()) {
                    extractTo.mkdir();
                    entry = zis.getNextEntry();
                    continue;
                } else {
                    extractTo.createNewFile();
                }
                fos = new FileOutputStream(extractTo);

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

                fos.close();
                entry = zis.getNextEntry();
            }
        } finally {
            zis.closeEntry();
            zis.close();
            fos.close();
        }
    }

    public static void mkdir(File f) {
        if (!f.exists()) {
            f.mkdir();
        }
    }
}

Related

  1. unzip(File zip, String folder, File into)
  2. unZip(File zipf, String targetDir)
  3. unzip(File zipFile, File dest)
  4. unzip(File zipFile, File destDir)
  5. unzip(File zipFile, File destDir)
  6. unzip(File zipFile, File destDir)
  7. unzip(File zipFile, File destDir)
  8. unzip(File zipFile, File destination)
  9. unzip(File zipFile, File destination, IProgressMonitor monitor)