Android Unzip File unZip(String zipfile, String destDir)

Here you can find the source of unZip(String zipfile, String destDir)

Description

un Zip

License

Apache License

Declaration

@SuppressWarnings("unchecked")
public static void unZip(String zipfile, String destDir)
        throws ZipException, IOException 

Method Source Code

//package com.java2s;
/*Copyright (C) 2012 Crow Hou (crow_hou@126.com)

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/

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

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Main {

    @SuppressWarnings("unchecked")
    public static void unZip(String zipfile, String destDir)
            throws ZipException, IOException {
        byte b[] = new byte[1024];
        int length;

        ZipFile zipFile = null;/*from   w  ww  .  j  a  va  2  s .c  om*/
        try {
            zipFile = new ZipFile(new File(zipfile));
            Enumeration<ZipEntry> enumeration = (Enumeration<ZipEntry>) zipFile
                    .entries();
            ZipEntry zipEntry = null;
            while (enumeration.hasMoreElements()) {
                zipEntry = enumeration.nextElement();
                File loadFile = new File(destDir, zipEntry.getName());
                if (zipEntry.isDirectory()) {
                    if (!loadFile.exists())
                        loadFile.mkdirs();
                } else {
                    if (!loadFile.getParentFile().exists()) {
                        loadFile.getParentFile().mkdirs();
                    }
                    OutputStream os = null;
                    InputStream is = null;
                    try {
                        os = new BufferedOutputStream(new FileOutputStream(
                                loadFile));
                        is = new BufferedInputStream(
                                zipFile.getInputStream(zipEntry));
                        while ((length = is.read(b)) != -1) {
                            os.write(b, 0, length);
                        }
                        os.flush();
                    } finally {
                        if (os != null)
                            os.close();
                        if (is != null)
                            is.close();
                    }
                }
            }
        } finally {
            if (zipFile != null)
                zipFile.close();
        }
    }

    public static void unZip(InputStream in, String destDir)
            throws FileNotFoundException, IOException {
        final int BUFFER = 1024;
        byte data[] = new byte[BUFFER];
        ZipInputStream zis = null;
        try {
            zis = new ZipInputStream(in);
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    File childDir = new File(destDir, entry.getName());
                    if (!childDir.exists()) {
                        childDir.mkdirs();
                    }
                } else {
                    File destFile = new File(destDir, entry.getName());
                    if (!destFile.getParentFile().exists()) {
                        destFile.getParentFile().mkdirs();
                    }
                    OutputStream dest = null;
                    try {
                        dest = new BufferedOutputStream(
                                new FileOutputStream(destFile));
                        int count;
                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
                            dest.write(data, 0, count);
                        }
                        dest.flush();
                    } finally {
                        if (dest != null)
                            dest.close();
                    }
                }
            }
        } finally {
            if (zis != null)
                zis.close();
        }
    }
}

Related

  1. upZipFile(String zipFile, String folderPath)
  2. upZipSelectedFile(File zipFile, String folderPath, String nameContains)
  3. upZipSelectedFile(File zipFile, String folderPath, String nameContains)
  4. unZip(File srcFile, File targetFile)
  5. unZip(String zipFile, String targetPath)
  6. UpZip(String zipFileString, String fileString)
  7. UpZip(String zipFileString, String fileString)
  8. UpZip(String zipFileString, String fileString)
  9. unzipArchive(File archive, File outputDir)