un Zip Folder - Android File Input Output

Android examples for File Input Output:Zip File

Description

un Zip Folder

Demo Code


//package com.java2s;

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 ww . j  a v 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 Tutorials