Android Unzip File unzip(String zipFile, String location)

Here you can find the source of unzip(String zipFile, String location)

Description

Unzip a zip file.

Parameter

Parameter Description
zipFile Full path of the zip file you'd like to unzip.
location Full path of the directory you'd like to unzip to (will be created if it doesn't exist).

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(String zipFile, String location)
        throws IOException 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 {
    private static int BUFFER_SIZE = 8192;

    /**/*from  w  w w.  ja va 2 s . c  o m*/
     * Unzip a zip file.  Will overwrite existing files.
     *
     * @param zipFile  Full path of the zip file you'd like to unzip.
     * @param location Full path of the directory you'd like to unzip to (will be created if it doesn't exist).
     * @throws IOException
     */
    public static void unzip(String zipFile, String location)
            throws IOException {
        int size;
        byte[] buffer = new byte[BUFFER_SIZE];

        if (!location.endsWith("/")) {
            location += "/";
        }
        File f = new File(location);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(new BufferedInputStream(
                new FileInputStream(zipFile), BUFFER_SIZE));
        try {
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                String path = location + ze.getName();
                File unzipFile = new File(path);

                if (ze.isDirectory()) {
                    if (!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    // check for and create parent directories if they don't exist
                    File parentDir = unzipFile.getParentFile();
                    if (null != parentDir) {
                        if (!parentDir.isDirectory()) {
                            parentDir.mkdirs();
                        }
                    }

                    // unzip the file
                    FileOutputStream out = new FileOutputStream(unzipFile,
                            false);
                    BufferedOutputStream fout = new BufferedOutputStream(
                            out, BUFFER_SIZE);
                    try {
                        while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) {
                            fout.write(buffer, 0, size);
                        }

                        zin.closeEntry();
                    } finally {
                        fout.flush();
                        fout.close();
                    }
                }
            }
        } finally {
            zin.close();
        }

    }
}

Related

  1. unzip(File zippedFile, File unpackedFile)
  2. unzip(File zippedFile, File unpackedFile)
  3. unzip(String zipFile, String location)
  4. unzip(String zipFile, String location)
  5. unzip(String zipFile, String location)
  6. unpack(File zippedFile, File unpackedFile)
  7. unpack(File zippedFile, File unpackedFile)
  8. unpackZip(File zip, String unpackPath)
  9. unpackZip(File zipFile, File location)