Android Unzip File unzip(File archive, File path)

Here you can find the source of unzip(File archive, File path)

Description

unzip

Declaration

public static void unzip(File archive, File path) throws IOException 

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

public class Main {
    public static void unzip(File archive, File path) throws IOException {
        ZipInputStream zip = null;
        String fileName = null;/*from  w  w  w  .  j a  va2 s  .c om*/
        try {
            if (!path.exists()) {
                path.mkdirs();
            }
            zip = new ZipInputStream(new FileInputStream(archive));
            ZipEntry zipEntry;
            while ((zipEntry = zip.getNextEntry()) != null) {
                fileName = zipEntry.getName();
                final File outputFile = new File(path, fileName);
                writeToStream(new BufferedInputStream(zip),
                        new FileOutputStream(outputFile), false);
                zip.closeEntry();
            }
            zip.close();
            zip = null;
        } finally {
            if (zip != null) {
                try {
                    zip.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public static void writeToStream(InputStream in, OutputStream out,
            boolean closeOnExit) throws IOException {
        byte[] bytes = new byte[2048];
        for (int c = in.read(bytes); c != -1; c = in.read(bytes)) {
            out.write(bytes, 0, c);
        }
        if (closeOnExit) {
            in.close();
            out.close();
        }
    }
}

Related

  1. unzip(File target, File dest)
  2. unzip(File target, File dest)
  3. unzip(File zip, File extractTo)
  4. unzip(File zipfile, File outputfolder)