Android Unzip File unzip(String[] namafileygdicrot, String zipFile, String location)

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

Description

unzip

Declaration

public static Boolean unzip(String[] namafileygdicrot, String zipFile,
            String location) 

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.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

public class Main {
    private static final int BUFFER_SIZE = 2048;

    public static Boolean unzip(String[] namafileygdicrot, String zipFile,
            String location) {//w w w.ja v a 2 s.  co  m
        boolean result = false;
        int size;
        byte[] buffer = new byte[BUFFER_SIZE];

        try {
            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();
                            }
                        }
                        if (!unzipFile.getName().equals("info.json")
                                && !unzipFile.getName().contains(
                                        "screenshot")
                                && isnamafileexist(unzipFile.getName(),
                                        namafileygdicrot)) {
                            // 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();
                            }
                        }
                    }
                }
                result = true;
            } finally {
                zin.close();
            }
        } catch (Exception e) {
            Log.e("", "Unzip exception", e);
            result = false;
        }
        return result;
    }

    public static Boolean unzip(String zipFile, String location) {
        boolean result = false;
        int size;
        byte[] buffer = new byte[BUFFER_SIZE];

        try {
            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();
                            }
                        }
                        if (!unzipFile.getName().equals("info.json")
                                && !unzipFile.getName().contains(
                                        "screenshot")) {
                            // 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();
                            }
                        }
                    }
                }
                result = true;
            } finally {
                zin.close();
            }
        } catch (Exception e) {
            Log.e("", "Unzip exception", e);
            result = false;
        }
        return result;
    }

    private static boolean isnamafileexist(String namafile, String[] nama) {
        boolean result = false;
        for (String s : nama) {
            if (namafile.contains(s))
                result = true;

        }
        return result;
    }
}

Related

  1. unpack(File zippedFile, File unpackedFile)
  2. unpackZip(File zip, String unpackPath)
  3. unpackZip(File zipFile, File location)
  4. unpackZip(String path, String zipname)
  5. unpackZip(String zipFile, String location)
  6. unzip(String[] namafileygdicrot, String zipFile, String location)
  7. unzip(final String zipFile, String outPath)
  8. upZip(String zipFilePath, String fileString)
  9. upZip(String zipFilePath, String fileString)