Android Unzip File unpackZip(String zipFile, String location)

Here you can find the source of unpackZip(String zipFile, String location)

Description

unpack Zip

License

Open Source License

Declaration

public static void unpackZip(String zipFile, String location) 

Method Source Code

//package com.java2s;
/*/* w  ww .j a v a 2  s .co  m*/
 * Copyright (C) 2014 Simple Explorer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    private static final int BUFFER = 4096;

    public static void unpackZip(String zipFile, String location) {
        try {
            File fSourceZip = new File(zipFile);
            // File temp = new File(zipPath);
            // temp.mkdir();

            // Extract entries while creating required sub-directories
            ZipFile zf = new ZipFile(fSourceZip);
            Enumeration<?> e = zf.entries();

            while (e.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                File destinationFilePath = new File(location,
                        entry.getName());

                // create directories if required.
                destinationFilePath.getParentFile().mkdirs();

                // if the entry is directory, leave it. Otherwise extract it.
                if (!entry.isDirectory()) {
                    // Get the InputStream for current entry of the zip file
                    // using InputStream getInputStream(Entry entry) method.
                    BufferedInputStream bis = new BufferedInputStream(
                            zf.getInputStream(entry));

                    int b;
                    byte buffer[] = new byte[BUFFER];

                    // read the current entry from the zip file, extract it and
                    // write the extracted file.
                    FileOutputStream fos = new FileOutputStream(
                            destinationFilePath);
                    BufferedOutputStream bos = new BufferedOutputStream(
                            fos, BUFFER);

                    while ((b = bis.read(buffer, 0, 1024)) != -1) {
                        bos.write(buffer, 0, b);
                    }

                    bos.flush();
                    bos.close();
                    bis.close();
                }
            }

            zf.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

Related

  1. unpack(File zippedFile, File unpackedFile)
  2. unpack(File zippedFile, File unpackedFile)
  3. unpackZip(File zip, String unpackPath)
  4. unpackZip(File zipFile, File location)
  5. unpackZip(String path, String zipname)
  6. unzip(String[] namafileygdicrot, String zipFile, String location)
  7. unzip(String[] namafileygdicrot, String zipFile, String location)
  8. unzip(final String zipFile, String outPath)
  9. upZip(String zipFilePath, String fileString)