Android Zip Unzip File unzip(File pZipFile)

Here you can find the source of unzip(File pZipFile)

Description

unzip

License

Apache License

Parameter

Parameter Description
pZipFile a parameter

Return

the list of unzipped files which where partialTopic of pZipFile

Declaration

public static Vector<String> unzip(File pZipFile) 

Method Source Code

/**// w  ww.  j  a v  a  2  s  . c  o  m
 Copyright 2014 Jens Glufke

 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.
 * -----------------------------------------------------------------------<br>
 * 
 * class that can be used to operations on files like copy, deleteDir etc 
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Main{
    private static final String LOGTAG = FileUtil.class.getSimpleName();
    private static final String TEMP_DIR_NAME = "/TEMP/";
    /**
     * 
     *
     * @param pZipFile
     * @return the list of unzipped files which where partialTopic of pZipFile
     *
     */
    public static Vector<String> unzip(File pZipFile) {
        /**
         * create a temp directory
         */
        File lTempDir = createTempDir();
        return unzipDirectory(lTempDir, pZipFile);
    }
    public static synchronized File createTempDir() {
        /**
         * get the name
         */
        String lFilename = "tempDir";
        File lTempDir = createTempFile(lFilename);
        if (!lTempDir.mkdir()) {
            throw new RuntimeException("could not create dir:"
                    + lTempDir.getAbsolutePath());
        }
        return lTempDir;
    }
    /**
     *
     *
     * @param p2BeUnzippedDir
     * @param pZipFile
     * @return the list of unzipped file names
     *
     */
    public static Vector<String> unzipDirectory(File p2BeUnzippedDir,
            File pZipFile) {
        if (p2BeUnzippedDir == null) {
            throw new IllegalArgumentException(
                    "parameter p2BeUnzippedDir must not be null");
        }
        if (!p2BeUnzippedDir.isDirectory()) {
            throw new IllegalArgumentException(
                    "parameter p2BeUnzippedDir has to be a directory");
        }
        if (pZipFile == null) {
            throw new IllegalArgumentException(
                    "parameter pZipFile must not be null");
        }
        if (pZipFile.isDirectory()) {
            throw new IllegalArgumentException(
                    "parameter pZipFile has to be a file and not a directory");
        }
        Vector<String> lFilenameList = new Vector<String>();
        try {
            ZipFile lZipFile = new ZipFile(pZipFile);
            Enumeration<? extends ZipEntry> lEntryEnum = lZipFile.entries();
            while (lEntryEnum.hasMoreElements()) {
                ZipEntry lEntry = lEntryEnum.nextElement();
                InputStream lIS = lZipFile.getInputStream(lEntry);
                BufferedInputStream lBIS = new BufferedInputStream(lIS);
                String lFilename = p2BeUnzippedDir.getAbsolutePath() + "/"
                        + lEntry.getName();
                new File(lFilename).getParentFile().mkdirs();
                FileOutputStream lFOS = new FileOutputStream(lFilename);
                BufferedOutputStream lBOS = new BufferedOutputStream(lFOS);
                FileUtil.copy(lBIS, lBOS);
                lBOS.close();
                lFilenameList.add(lFilename);
            }
            lZipFile.close();
        } catch (IOException lE) {
            LogWrapper.e(LOGTAG,
                    "can not unzip file:" + pZipFile.getAbsolutePath()
                            + " to:" + p2BeUnzippedDir);
        }
        return lFilenameList;
    }
    /**
     * 
     * @param pFilename
     * @return
     */
    public static synchronized File createTempFile(String pFilename) {
        String lPrefix = TEMP_DIR_NAME + pFilename;
        int lSuffix = 1;
        String lFilename = lPrefix;
        File lTempFile = null;
        while ((lTempFile = new File(lFilename)).exists()) {
            lFilename = lPrefix + lSuffix++;
        }
        lTempFile.getParentFile().mkdirs();
        lTempFile.deleteOnExit();

        return lTempFile;
    }
    /**
     * 
     * @param pSourceFile
     * @param pTargetFile
     * @throws IOException
     */
    public static synchronized void copy(File pSourceFile, File pTargetFile)
            throws IOException {

        if (!pSourceFile.getCanonicalPath().equals(
                pTargetFile.getCanonicalPath())) {

            pTargetFile.getParentFile().mkdirs();

            FileInputStream lFIS = new FileInputStream(pSourceFile);
            FileOutputStream lFOS = new FileOutputStream(
                    pTargetFile.getCanonicalPath());
            copy(lFIS, lFOS);
            lFOS.close();
        }
    }
    /**
     * destination is not closed on exit of method !
     * @param pSourceStream
     * @param pTargetStream
     * @throws IOException
     */
    public static synchronized void copy(InputStream pSourceStream,
            OutputStream pTargetStream) throws IOException {
        copy(pSourceStream, pTargetStream, true);
    }
    /**
     * destination is not closed on exit of method !
     * @param pSourceStream
     * @param pTargetStream
     * @param closeSource define if the source id closed after copying
     * @throws IOException
     */
    public static synchronized void copy(InputStream pSourceStream,
            OutputStream pTargetStream, boolean closeSource)
            throws IOException {

        final int buffSize = 1024;
        final byte[] lBuf = new byte[buffSize];

        int lBytesRead = 0;
        while ((lBytesRead = pSourceStream.read(lBuf, 0, buffSize)) != -1) {
            pTargetStream.write(lBuf, 0, lBytesRead);
        }
        if (closeSource) {
            pSourceStream.close();
        }
    }
    /**
     * 
     * @param pSourceFile
     * @param pTargetFile
     * @throws IOException
     */
    public static synchronized void copy(String pSourceFile,
            String pTargetFile) throws IOException {

        copy(new File(pSourceFile), new File(pTargetFile));
    }
    /**
     * 
     * @param pFile
     * @return
     */
    public static String getCanonicalPath(File pFile) {

        String lReturnPath = pFile.getAbsolutePath();
        lReturnPath = lReturnPath.replaceAll("\\\\", "/");

        final String regExp1 = "()[^/]+/(\\.\\.)";
        final Pattern pattern1 = Pattern.compile(regExp1);
        final String regExp2 = "([^/]+/)(\\.)";
        final Pattern pattern2 = Pattern.compile(regExp2);
        final String regExp3 = "(/)(/)";
        final Pattern pattern3 = Pattern.compile(regExp3);

        for (Pattern lPattern : new Pattern[] { pattern1, pattern2,
                pattern3 }) {
            Matcher lMatcher = lPattern.matcher(lReturnPath);
            while (lMatcher.find()) {
                lReturnPath = lMatcher.replaceFirst(lMatcher.group(1));
                lMatcher = lPattern.matcher(lReturnPath);
            }
        }
        if (lReturnPath.endsWith("/")) {
            lReturnPath = lReturnPath
                    .substring(0, lReturnPath.length() - 1);
        }
        return lReturnPath;
    }
}

Related

  1. gUnzip(File srcFile, File destFile)
  2. unJarDirectory(final String in, final File fOut)
  3. unzip(String sourceFileName, String destPath)
  4. zip(String filePath, String zipPath)
  5. zip(String sourceDir, String zipFile)
  6. zip(String[] files, String destZipFilePath)