Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 {
    /**
     * 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();
        }
    }
}