Android Unzip File unZipFromAssets(Context ctx, String file, String destinationDirectory)

Here you can find the source of unZipFromAssets(Context ctx, String file, String destinationDirectory)

Description

extracts a zip archive from the assets dir.

Parameter

Parameter Description
ctx TODO
destinationDirectory TODO
argv a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static void unZipFromAssets(Context ctx, String file,
        String destinationDirectory) throws Exception 

Method Source Code

//package com.java2s;

import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class Main {
    /**//  w  w w.j a  v a  2 s . c o  m
     * extracts a zip archive from the assets dir. Copies to a writable dir first.
     * @param ctx TODO
     * @param destinationDirectory TODO
     * @param argv
     * @throws Exception 
     */
    public static void unZipFromAssets(Context ctx, String file,
            String destinationDirectory) throws Exception {
        String destinationFilename = extractFromAssets(ctx, file,
                destinationDirectory);
        try {
            unZip(destinationFilename, destinationDirectory);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public static String extractFromAssets(Context ctx, String file,
            String destinationDirectory) throws IOException,
            FileNotFoundException {
        final int BUFFER = 2048;
        BufferedOutputStream dest = null;
        AssetManager assetManager = ctx.getAssets();
        InputStream in = assetManager.open(file);
        String destinationFilename = destinationDirectory + File.separator
                + file;
        OutputStream out = new FileOutputStream(destinationFilename);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        out.close();
        return destinationFilename;
    }

    /**
     * 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. unzipArchive(File archive, File outputDir)
  3. UnZipFileToMem(String source)
  4. UnZipFile(String source, String targetPath)
  5. unZip(String zipFile, String destinationDirectory)