Java Unzip File unzip(File src, File dest)

Here you can find the source of unzip(File src, File dest)

Description

Unzip the the file at the specified location.

License

Apache License

Parameter

Parameter Description
src The file to unzip
dest The destination folder

Exception

Parameter Description
IOException when IO error occur.

Declaration

public static void unzip(File src, File dest) throws 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.ZipFile;

public class Main {
    /**//from  ww  w .java  2  s.co m
     * Size of internal buffer.
     */
    private static final int BUFFER = 2048;

    /**
     * Unzip the the file at the specified location.
     *
     * @param src  The file to unzip
     * @param dest The destination folder
     * @throws IOException when IO error occur.
     */
    public static void unzip(File src, File dest) throws IOException {
        dest.mkdirs();
        BufferedOutputStream out = null;
        BufferedInputStream is = null;
        try {
            ZipEntry entry;
            ZipFile zip = new ZipFile(src);
            for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements();) {
                entry = e.nextElement();
                String name = entry.getName();
                // Ensure that the folder exists
                if (name.indexOf('/') > 0) {
                    String folder = name.substring(0, name.lastIndexOf('/'));
                    File dir = new File(dest, folder);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                }
                // Only process files
                if (!entry.isDirectory()) {
                    is = new BufferedInputStream(zip.getInputStream(entry));
                    int count;
                    byte[] data = new byte[BUFFER];
                    File f = new File(dest, name);
                    try (FileOutputStream fos = new FileOutputStream(f)) {
                        out = new BufferedOutputStream(fos, BUFFER);
                        while ((count = is.read(data, 0, BUFFER)) != -1) {
                            out.write(data, 0, count);
                        }
                        out.flush();
                    }
                    is.close();
                }
            }
            zip.close();
        } finally {
            // If the ZIP is empty these may be null
            if (is != null) {
                is.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. unzip(File intoFolder, ZipFile zipFile)
  2. unzip(File jar, File target)
  3. unzip(File jarFile, File destDir)
  4. unzip(File sourceFile, File rootDir)
  5. unzip(File sourceZipfile, File directory)
  6. unzip(File src, File target)
  7. unzip(File srcZipFile, File tgtDir)
  8. unzip(File tarFile, File destinationDir)
  9. unzip(File target)