Android Unzip Folder unZipFolder(InputStream zipFileStream, String outPathString)

Here you can find the source of unZipFolder(InputStream zipFileStream, String outPathString)

Description

un Zip Folder

License

Apache License

Declaration

public final static void unZipFolder(InputStream zipFileStream,
            String outPathString) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public final static void unZipFolder(InputStream zipFileStream,
            String outPathString) {
        ZipInputStream inZip = null;
        FileOutputStream out = null;
        try {/*from w  w  w . jav  a 2  s. c om*/
            inZip = new ZipInputStream(zipFileStream);
            ZipEntry zipEntry;
            String szName = "";
            while ((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();

                if (zipEntry.isDirectory()) {

                    // get the folder name of the widget
                    szName = szName.substring(0, szName.length() - 1);
                    java.io.File folder = new java.io.File(outPathString
                            + java.io.File.separator + szName);
                    folder.mkdirs();

                } else {

                    java.io.File file = new java.io.File(outPathString
                            + java.io.File.separator + szName);
                    file.createNewFile();
                    // get the output stream of the file
                    out = new FileOutputStream(file);
                    int len;
                    byte[] buffer = new byte[1024];
                    // read (len) bytes into buffer
                    while ((len = inZip.read(buffer)) > 0) {
                        // write (len) byte from buffer at the position 0
                        out.write(buffer, 0, len);
                        out.flush();
                    }
                    out.close();
                }
            }// end of while

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (inZip != null) {
                try {
                    inZip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

  1. UnZipFolder(String zipFileString, String outPathString)
  2. UnZipFolder(String zipFileString, String outPathString)
  3. UnzipSubDir(InputStream is, String subdir, String targetPath)
  4. UnzipSubDir(String source, String subdir, String targetPath)
  5. unZipFolder(InputStream zipFileStream, String outPathString)
  6. unZipNoDirs(String file, String destinationDirectory)