Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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;

import android.os.Environment;

public class Main {
    public static String unzip(String filename) throws IOException {
        BufferedOutputStream origin = null;
        String path = null;
        Integer BUFFER_SIZE = 20480;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

            File flockedFilesFolder = new File(
                    Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
            System.out.println("FlockedFileDir: " + flockedFilesFolder);
            String compressedFile = flockedFilesFolder.toString() + "/" + filename;
            System.out.println("compressed File name:" + compressedFile);
            //String uncompressedFile = flockedFilesFolder.toString()+"/"+"flockUnZip";
            File purgeFile = new File(compressedFile);

            try {
                ZipInputStream zin = new ZipInputStream(new FileInputStream(compressedFile));
                BufferedInputStream bis = new BufferedInputStream(zin);
                try {
                    ZipEntry ze = null;
                    while ((ze = zin.getNextEntry()) != null) {
                        byte data[] = new byte[BUFFER_SIZE];
                        path = flockedFilesFolder.toString() + "/" + ze.getName();
                        System.out.println("path is:" + path);
                        if (ze.isDirectory()) {
                            File unzipFile = new File(path);
                            if (!unzipFile.isDirectory()) {
                                unzipFile.mkdirs();
                            }
                        } else {
                            FileOutputStream fout = new FileOutputStream(path, false);
                            origin = new BufferedOutputStream(fout, BUFFER_SIZE);
                            try {
                                /* for (int c = bis.read(data, 0, BUFFER_SIZE); c != -1; c = bis.read(data)) {
                                origin.write(c);
                                 }
                                 */
                                int count;
                                while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
                                    origin.write(data, 0, count);
                                }
                                zin.closeEntry();
                            } finally {
                                origin.close();
                                fout.close();

                            }
                        }
                    }
                } finally {
                    bis.close();
                    zin.close();
                    purgeFile.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return path;
        }
        return null;
    }
}