Android Unzip File unpackZip(File zip, String unpackPath)

Here you can find the source of unpackZip(File zip, String unpackPath)

Description

Unzip the specified file from a file Be sure to set : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> Modified from: Source: http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android

Parameter

Parameter Description
zip target zip file
unpackPath path where file should be unpacked

Declaration

public static boolean unpackZip(File zip, String unpackPath) 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**/*from w  w  w.jav a 2  s .c  o m*/
     * Unzip the specified file from a file 
     * 
     * Be sure to set : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     * 
     * Modified from:  
     * Source: http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android
     * 
     * @param zip target zip file
     * @param unpackPath path where file should be unpacked
     * @return
     */
    public static boolean unpackZip(File zip, String unpackPath) {

        InputStream is;
        ZipInputStream zis;
        try {

            String filename;
            is = new FileInputStream(zip);
            zis = new ZipInputStream(new BufferedInputStream(is));
            ZipEntry ze;
            byte[] buffer = new byte[1024];
            int count;

            while ((ze = zis.getNextEntry()) != null) {

                filename = ze.getName();
                FileOutputStream fout = new FileOutputStream(unpackPath
                        + filename);

                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }

                fout.close();
                zis.closeEntry();
            }

            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * Unzip the specified file from a file 
     * 
     * Be sure to set : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     * 
     * Modified from:  
     * Source: http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android
     * 
     * @param file input stream 
     * @param unpackPath path where file should be unpacked
     * @return
     */
    public static boolean unpackZip(InputStream zipStream, String unpackPath) {

        InputStream is;
        ZipInputStream zis;
        try {

            String filename;
            zis = new ZipInputStream(new BufferedInputStream(zipStream));
            ZipEntry ze;
            byte[] buffer = new byte[1024];
            int count;

            while ((ze = zis.getNextEntry()) != null) {

                filename = ze.getName();
                FileOutputStream fout = new FileOutputStream(unpackPath
                        + filename);

                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }

                fout.close();
                zis.closeEntry();
            }

            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. unzip(String zipFile, String location)
  2. unzip(String zipFile, String location)
  3. unzip(String zipFile, String location)
  4. unpack(File zippedFile, File unpackedFile)
  5. unpack(File zippedFile, File unpackedFile)
  6. unpackZip(File zipFile, File location)
  7. unpackZip(String path, String zipname)
  8. unpackZip(String zipFile, String location)
  9. unzip(String[] namafileygdicrot, String zipFile, String location)