extract Zip Archive - Android File Input Output

Android examples for File Input Output:Zip File

Description

extract Zip Archive

Demo Code


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.content.Context;
import android.os.Environment;

public class Main{
    public static final int DEFAULT_BUFFER_SIZE = 2048;
    public static boolean extractZipArchive(File zipArchive,
            File targetDirectory, boolean maintainFolderStructure,
            boolean overwrite) {
        if ((!zipArchive.exists()) || (!zipArchive.isFile()))
            return false;
        if (!targetDirectory.exists())
            targetDirectory.mkdirs();//from w ww  . j  a  v  a2  s. c o  m
        if (!targetDirectory.isDirectory())
            return false;

        ZipInputStream zipIn = null;
        BufferedOutputStream output = null;
        String targetPath = targetDirectory.getAbsolutePath() + "/";

        try {
            zipIn = new ZipInputStream(new BufferedInputStream(
                    new FileInputStream(zipArchive)));

            ZipEntry entry = null;

            while ((entry = zipIn.getNextEntry()) != null) {

                String entryName = entry.getName();

                if (entry.isDirectory()) {
                    if (maintainFolderStructure) {
                        File dir = new File(targetPath + entryName);
                        dir.mkdirs();
                    }
                } else {
                    File targetFile = null;
                    if (maintainFolderStructure) {
                        targetFile = new File(targetPath + entryName);
                        if (!targetFile.getParentFile().exists()) {
                            targetFile.getParentFile().mkdirs();
                        }
                    } else {
                        targetFile = new File(targetPath
                                + entryName.substring(entryName
                                        .lastIndexOf("/") + 1));
                    }

                    copyInputStreamToFile(zipIn, targetFile,
                            DEFAULT_BUFFER_SIZE, false, overwrite, null);
                }
            }

            zipIn.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            closeStreams(zipIn, output, true, true);
            return false;
        }

    }
    public static void copyInputStreamToFile(InputStream from, File to,
            int bufferSize, boolean closeInput, boolean overwrite,
            ProgressListener progressListener) {
        if (!to.getParentFile().exists())
            to.getParentFile().mkdirs();
        if (overwrite && to.exists()) {
            recursiveDelete(to);
        }
        if (!to.exists()) {
            try {
                to.createNewFile();
                copyInputStreamToOutputStream(from,
                        new BufferedOutputStream(new FileOutputStream(to),
                                bufferSize), bufferSize, closeInput, true,
                        progressListener);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
    public static void closeStreams(InputStream from, OutputStream to,
            boolean closeInput, boolean closeOutput) {
        try {
            if (to != null)
                to.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (closeInput && from != null)
                from.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (closeOutput && to != null)
                to.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void recursiveDelete(File file) {
        if (!file.exists())
            return;
        if (file.isDirectory()) {
            File[] contents = file.listFiles();
            for (int i = 0; i < contents.length; i++)
                recursiveDelete(contents[i]);
        }
        try {
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void recursiveDelete(String filePath) {
        recursiveDelete(new File(filePath));
    }
    public static void copyInputStreamToOutputStream(InputStream from,
            OutputStream to, int bufferSize, boolean closeInput,
            boolean closeOutput, ProgressListener progressListener) {
        try {
            int totalBytesRead = 0;
            int bytesRead = 0;
            int offset = 0;
            byte[] data = new byte[bufferSize];

            while ((bytesRead = from.read(data, offset, bufferSize)) > 0) {
                totalBytesRead += bytesRead;
                to.write(data, offset, bytesRead);
                if (progressListener != null)
                    progressListener.onProgressUpdate(totalBytesRead);
                //            Log.d(TAG, "Copied " + totalBytesRead + " bytes");
            }
            closeStreams(from, to, closeInput, closeOutput);
        } catch (Exception e) {
            closeStreams(from, to, closeInput, closeOutput);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials