Android Unzip File unZip(String zipFile, String destinationDirectory)

Here you can find the source of unZip(String zipFile, String destinationDirectory)

Description

kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)

Parameter

Parameter Description
zipFile a parameter
destinationDirectory a parameter

Declaration

public static void unZip(String zipFile, String destinationDirectory) 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

public class Main {
    /**//  w  w  w. j a v a2  s  . c  om
     * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
     * @param zipFile
     * @param destinationDirectory
     */
    public static void unZip(String zipFile, String destinationDirectory) {
        try {
            FileInputStream fin = new FileInputStream(zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());
                String destinationPath = destinationDirectory
                        + File.separator + ze.getName();
                if (ze.isDirectory()) {
                    dirChecker(destinationPath);
                } else {
                    FileOutputStream fout;
                    try {
                        File outputFile = new File(destinationPath);
                        if (!outputFile.getParentFile().exists()) {
                            dirChecker(outputFile.getParentFile().getPath());
                        }
                        fout = new FileOutputStream(destinationPath);
                        for (int c = zin.read(); c != -1; c = zin.read()) {
                            fout.write(c);
                        }
                        zin.closeEntry();
                        fout.close();
                    } catch (Exception e) {
                        // ok for now.
                        Log.v("Decompress", "Error: " + e.getMessage());
                    }
                }
            }
            zin.close();
        } catch (Exception e) {
            Log.e("Decompress", "unzip", e);
        }
    }

    private static void dirChecker(String destinationPath) {
        File f = new File(destinationPath);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }
}

Related

  1. UpZip(String zipFileString, String fileString)
  2. UpZip(String zipFileString, String fileString)
  3. unzipArchive(File archive, File outputDir)
  4. UnZipFileToMem(String source)
  5. UnZipFile(String source, String targetPath)
  6. unZipFromAssets(Context ctx, String file, String destinationDirectory)