Android Unzip File UnZipFile(String source, String targetPath)

Here you can find the source of UnZipFile(String source, String targetPath)

Description

Unzip a source to the targetPath, is similar with the UnZipFile(InputStream is, String targetPath)

Parameter

Parameter Description
source source file to unzip
targetPath targetPath to place out files.

Return

true if successfully, false if failed. false if the source file is not exists.

Declaration

public static boolean UnZipFile(String source, String targetPath) 

Method Source Code

//package com.java2s;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;

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

public class Main {
    private static final int BUFFER_SIZE = 8192;
    protected static byte buf[] = new byte[BUFFER_SIZE];
    private static List<Object> mTmpBuffer = new ArrayList<Object>();

    /**/*  ww  w .jav a 2 s.  c o m*/
     * Unzip a inputstream data to the target path. if the path is exists, and
     * is not a dir will delete the path, and recreate it. if the path is not
     * exists, create the path. if the inputstream is not a package contain some
     * files and dirs, will out put the data to the target and create all the
     * files and dirs and the targetPath.
     * 
     * @param is
     *            is InputStream contain the files data.
     * @param targetPath
     *            targetPath must be end with system path seprator
     * @return true if unzip successflly, false if failed. false if the
     *         inputstream is null or the targetPath is null or the targetPath
     *         is empty string.
     */
    public static boolean UnZipFile(InputStream is, String targetPath) {
        if (is == null || targetPath.equals("") || targetPath == null) {
            return false;
        }
        try {
            checkDirectory(targetPath);
            extZipFile(is, targetPath);
            is.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Unzip a source to the targetPath, is similar with the
     * UnZipFile(InputStream is, String targetPath)
     * 
     * @param source
     *            source file to unzip
     * @param targetPath
     *            targetPath to place out files.
     * @return true if successfully, false if failed. false if the source file
     *         is not exists.
     */
    public static boolean UnZipFile(String source, String targetPath) {
        File file = new File(source);
        if (file.exists() == false) {
            return false;
        }
        try {
            return UnZipFile(new FileInputStream(file), targetPath);
        } catch (Exception e) {
        }
        return false;
    }

    private static boolean checkDirectory(String dir) {
        File dirObj = new File(dir);
        if (dirObj.exists()) {
            if (!dirObj.isDirectory()) {
                dirObj.delete();
            }
            return false;
        }
        if (!dirObj.exists()) {
            dirObj.mkdirs();
        }
        return true;
    }

    private static void extZipFile(InputStream is, String extPlace) {

        ZipInputStream in = new ZipInputStream(is);
        ZipEntry entry = null;

        try {
            while ((entry = in.getNextEntry()) != null) {
                final String fullName = extPlace + entry.getName();
                if (entry.isDirectory()) {
                    File file = new File(fullName);
                    file.mkdirs();
                } else {
                    doOutputFile(in, fullName);
                    in.closeEntry();
                }
            }
        } catch (IOException e) {
        } finally {
            in = null;
        }
    }

    private static void doOutputFile(InputStream is, String filename)
            throws IOException, FileNotFoundException {
        FileOutputStream os = new FileOutputStream(filename);
        BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE);
        int len;
        while ((len = is.read(buf, 0, BUFFER_SIZE)) > 0) {
            bos.write(buf, 0, len);
        }
        bos.flush();
        bos.close();
        os.close();
        mTmpBuffer.add(os);
        mTmpBuffer.add(bos);
    }
}

Related

  1. UpZip(String zipFileString, String fileString)
  2. UpZip(String zipFileString, String fileString)
  3. UpZip(String zipFileString, String fileString)
  4. unzipArchive(File archive, File outputDir)
  5. UnZipFileToMem(String source)
  6. unZip(String zipFile, String destinationDirectory)
  7. unZipFromAssets(Context ctx, String file, String destinationDirectory)