Java Unzip File unzip(File zipFile, File destDir)

Here you can find the source of unzip(File zipFile, File destDir)

Description

unzip

License

Open Source License

Parameter

Parameter Description
zipFile the File to open as a ZipFile
destDir the File to copy contents of zipFile to (need not exist)

Exception

Parameter Description
IOException an exception

Return

String error if detected or null otherwise

Declaration

public static String unzip(File zipFile, File destDir) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 eBay Inc./*from w w  w . ja  v  a2  s . c  o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *******************************************************************************/

import java.io.ByteArrayOutputStream;
import java.io.File;
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.ZipFile;

public class Main {
    private static final int DEFAULT_BUFFER = 4096;
    private static final int MAX_STREAM_COPY_SIZE = 1024 * 1024 * 50;

    /**
     * @param zipFile
     *            the File to open as a ZipFile
     * @param destDir
     *            the File to copy contents of zipFile to (need not exist)
     * @return String error if detected or null otherwise
     * @throws IOException
     */
    public static String unzip(File zipFile, File destDir) throws IOException {
        if (!isDirectoryWritable(destDir, true)) {
            return "unable to create writable directory: " + destDir;
        }

        ZipFile zip = null;
        try {
            zip = new ZipFile(zipFile);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    File destFile = new File(destDir, entry.getName());
                    InputStream src = null;
                    try {
                        src = zip.getInputStream(entry);
                        String result = write(src, destFile);
                        if (null != result) {
                            return result;
                        }
                    } finally {
                        if (null != src) {
                            src.close();
                        }
                    }
                }
            }
        } finally {
            if (null != zip) {
                zip.close();
            }
        }
        return null;
    }

    public static boolean isDirectoryWritable(File file) {
        return (null != file) && file.canWrite() && file.isDirectory();
    }

    public static boolean isDirectoryWritable(File dir, boolean force) {
        if (isDirectoryWritable(dir)) {
            return true;
        }
        if (!force) {
            return false;
        }
        dir.mkdirs(); // result too variable to rely on
        return isDirectoryWritable(dir);
    }

    public static String write(InputStream source, File destFile) throws IOException {

        if (!isDirectoryWritable(destFile.getParentFile(), true)) {
            return "unable to create parent dir for " + destFile;
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(destFile);
            copy(source, out);
            return null;
        } finally {
            if (null != out) {
                out.close();
            }
        }
    }

    public static String copy(InputStream inputStream) throws IOException {
        return copy(inputStream, MAX_STREAM_COPY_SIZE);
    }

    public static String copy(InputStream inputStream, int maxSize) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copy(inputStream, out, maxSize);
        return out.toString(); // TODO P3 assumes default encoding
    }

    public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
        copy(inputStream, outputStream, MAX_STREAM_COPY_SIZE);
    }

    public static void copy(InputStream inputStream, OutputStream outputStream, int maxSize) throws IOException {
        if (null != inputStream) {
            byte[] buffer = new byte[DEFAULT_BUFFER];
            int total = 0;
            int read;
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
                total += read;
            }
        }
    }
}

Related

  1. unZip(File zipf, String targetDir)
  2. unzip(File zipFile, File dest)
  3. unzip(File zipFile, File destDir)
  4. unzip(File zipFile, File destDir)
  5. unzip(File zipFile, File destDir)
  6. unzip(File zipFile, File destDir)
  7. unzip(File zipFile, File destination)
  8. unzip(File zipFile, File destination, IProgressMonitor monitor)
  9. unzip(File zipfile, File directory)