Android Unzip File unzip(File target, File dest)

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

Description

unzip

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 {
    public static void unzip(File target, File dest) {
        try {/*from w  w  w  .  j av  a 2 s  .  c  o m*/
            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();
            zis.close();
        } 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)
  6. unzip(File zippedFile, File unpackedFile)