Android Unzip File unzip(File target, File dest)

Here you can find the source of unzip(File target, File dest)

Description

unzip the target file to specific destination

Parameter

Parameter Description
target : target file, should be a zip file
dest : destination folder

Declaration

public static void unzip(File target, File dest) 

Method Source Code

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

public class Main {
    /**/*from   ww  w  . java2s  .co m*/
     * unzip the target file to specific destination
     * @param target : target file, should be a zip file
     * @param dest : destination folder
     */
    public static void unzip(File target, File dest) {
        try {
            if (!dest.exists()) {
                dest.mkdir();
            }
            ZipInputStream zis = new ZipInputStream(new FileInputStream(
                    target));
            ZipEntry nextEntry = null;
            while ((nextEntry = zis.getNextEntry()) != null) {
                String name = nextEntry.getName();
                File file = new File(dest, name);
                if (nextEntry.isDirectory()) {
                    file.mkdirs();
                } else {
                    File parentFolder = file.getParentFile();
                    if (!parentFolder.exists()) {
                        parentFolder.mkdirs();
                    }
                    file.createNewFile();
                    FileOutputStream fos = new FileOutputStream(file);
                    byte[] b = new byte[1024];
                    int count = 0;
                    while ((count = zis.read(b)) != -1) {
                        fos.write(b, 0, count);
                    }
                    fos.flush();
                    fos.close();
                }
            }
            zis.closeEntry();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. unzip(File archive, File path)
  2. unzip(File target, File dest)
  3. unzip(File zip, File extractTo)
  4. unzip(File zipfile, File outputfolder)
  5. unzip(File zippedFile, File unpackedFile)