Java Unzip File unzip(File file, File destDir)

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

Description

Unzips the zip file "file" into the directory "dest"

License

Open Source License

Parameter

Parameter Description
file The zip file
destDir Directory where the content of the zip file will be saved

Declaration

public static void unzip(File file, File destDir) 

Method Source Code


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

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

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

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

public class Main {
    /**/*  w  w w.j a  v  a  2s  .c  o  m*/
     * Unzips the zip file "file" into the directory "dest"
     * @param file The zip file
     * @param destDir Directory where the content of the zip file will be saved
     */
    public static void unzip(File file, File destDir) {
        destDir.mkdir();
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry ze = (ZipEntry) entries.nextElement();
                if (ze.isDirectory())
                    (new File(destDir, ze.getName())).mkdir();
                else {
                    // make sure directories exist in case the client
                    // didn't provide directory entries!

                    File f = new File(destDir, ze.getName());
                    (new File(f.getParent())).mkdirs();

                    FileOutputStream fos = null;
                    BufferedOutputStream bos = null;
                    InputStream in = null;
                    try {
                        fos = new FileOutputStream(f);
                        bos = new BufferedOutputStream(fos);
                        in = zipFile.getInputStream(ze);
                        copystream(in, bos);
                    } finally {
                        if (bos != null)
                            bos.close();
                        else if (fos != null)
                            fos.close();
                        if (in != null)
                            in.close();
                    }
                }
            }
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
    }

    /**
     * Copies the input stream to the output stream using a 1 kB buffer
     * @throws IOException
     */
    private static void copystream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);
    }
}

Related

  1. unzip(File archiveFile, File targetDir, boolean skipRoot)
  2. unzip(File dest, String jar)
  3. unzip(File destDir, InputStream is)
  4. unzip(File epubfile, File destination)
  5. unzip(File file, File dest)
  6. unzip(File file, File directory)
  7. unzip(File file, File targetDirectory)
  8. unzip(File file, File targetDirectory)
  9. unzip(File file, File toDir)