Android Unzip File unzipArchive(File archive, File outputDir)

Here you can find the source of unzipArchive(File archive, File outputDir)

Description

unzip Archive

Declaration

public static boolean unzipArchive(File archive, File outputDir) 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static boolean unzipArchive(File archive, File outputDir) {
        boolean result = false;
        ZipFile zipfile;//from w  w w.  j  av  a2 s  .c o m
        try {
            zipfile = new ZipFile(archive);
            for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, outputDir);
            }
            result = true;
        } catch (IOException e1) {
            // ignore
        }
        return result;
    }

    private static void unzipEntry(ZipFile zipfile, ZipEntry entry,
            File outputDir) throws IOException {

        if (entry.isDirectory()) {
            createDir(new File(outputDir, entry.getName()));
            return;
        }

        File outputFile = new File(outputDir, entry.getName());
        if (!outputFile.getParentFile().exists()) {
            createDir(outputFile.getParentFile());
        }

        BufferedInputStream inputStream = new BufferedInputStream(
                zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(
                new FileOutputStream(outputFile));

        try {
            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                outputStream.write(buf, 0, len);
            }
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    private static void createDir(File dir) throws IOException {
        if (!dir.mkdirs())
            throw new IOException("Can not create dir " + dir);
    }
}

Related

  1. unZip(String zipFile, String targetPath)
  2. unZip(String zipfile, String destDir)
  3. UpZip(String zipFileString, String fileString)
  4. UpZip(String zipFileString, String fileString)
  5. UpZip(String zipFileString, String fileString)
  6. UnZipFileToMem(String source)
  7. UnZipFile(String source, String targetPath)
  8. unZip(String zipFile, String destinationDirectory)
  9. unZipFromAssets(Context ctx, String file, String destinationDirectory)