Java Unzip File unzip(File zip, File location)

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

Description

Unzips a zip compressed file to a specific directory

License

Open Source License

Parameter

Parameter Description
zip The zip compressed file
location The location to unzip into

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

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

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

public class Main {
    /**//  ww  w .j  av a2s . c  o m
     * Unzips a zip compressed file to a specific directory
     * 
     * @param zip
     *       The zip compressed file
     * @param location
     *       The location to unzip into
     * @throws IOException
     */
    public static void unzip(File zip, File location) throws IOException {
        FileInputStream zipFileInput = new FileInputStream(zip.getAbsoluteFile());
        ZipInputStream zipInput = new ZipInputStream(zipFileInput);
        ZipEntry entry = null;
        String baseDir = null;
        if (location == null) {
            baseDir = zip.getParentFile().getAbsolutePath();
        } else {
            baseDir = location.getAbsolutePath();
        }
        while ((entry = zipInput.getNextEntry()) != null) {
            String name = entry.getName();
            File outFile = new File(baseDir + File.separator + name);
            if (entry.isDirectory()) {
                outFile.mkdirs();
            } else {
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdirs();
                }
                outFile.createNewFile();
                BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(outFile));
                copyStreams(zipInput, fileOut);
                fileOut.flush();
                fileOut.close();
            }
        }
        zipInput.close();
    }

    /**
     * Copies the contents of an input stream into an output stream
     * 
     * @param input
     * @param output
     * @throws IOException
     */
    private static void copyStreams(InputStream input, OutputStream output) throws IOException {
        byte[] temp = new byte[8192];
        int read = -1;
        while ((read = input.read(temp)) != -1) {
            output.write(temp, 0, read);
        }
    }
}

Related

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