Java Unzip File unzip(File zippedFile, File outputDirectory)

Here you can find the source of unzip(File zippedFile, File outputDirectory)

Description

Unzips a file to an output directory

License

Apache License

Parameter

Parameter Description
zippedFile a parameter
outputDirectory a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void unzip(File zippedFile, File outputDirectory) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  w  w  . j a v a 2 s . c o  m*/
 * Copyright 2011 cruxframework.org.
 * 
 * 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.ByteArrayOutputStream;
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.io.OutputStream;

import java.util.HashSet;

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

public class Main {
    /**
     * Unzips a file to an output directory 
     * @param zippedFile
     * @param outputDirectory
     * @throws IOException
     */
    public static void unzip(File zippedFile, File outputDirectory) throws IOException {
        Set<String> added = new HashSet<String>();

        ZipInputStream inStream = null;

        try {
            inStream = new ZipInputStream(new FileInputStream(zippedFile));

            ZipEntry entry;
            byte[] buffer = new byte[1024];

            while ((entry = inStream.getNextEntry()) != null) {
                String name = entry.getName();
                if (name != null && name.length() > 0 && !added.contains(name)) {
                    File outputFile = new File(outputDirectory, name);

                    if (entry.isDirectory()) {
                        outputFile.mkdirs();
                    } else {
                        extractFileFromZip(inStream, buffer, outputFile);
                    }

                    added.add(name);
                }
            }

            inStream.close();
        } finally {
            if (inStream != null) {
                inStream.close();
            }
        }
    }

    /**
     * Extracts a zip entry for a physical file
     * @param inStream
     * @param buffer
     * @param outputFile
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void extractFileFromZip(ZipInputStream inStream, byte[] buffer, File outputFile)
            throws FileNotFoundException, IOException {
        int nrBytesRead;
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }

        OutputStream outStream = new FileOutputStream(outputFile);
        try {
            while ((nrBytesRead = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, nrBytesRead);
            }
        } finally {
            if (outStream != null) {
                outStream.close();
            }
        }
    }

    /**
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static String read(File file) throws IOException {
        return read(new FileInputStream(file));
    }

    /**
     * @param in
     * @return
     * @throws IOException
     */
    public static String read(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int read = 0;
        byte[] buff = new byte[1024];
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        in.close();
        out.flush();
        out.close();

        return new String(out.toByteArray());
    }

    /**
     * @param text
     * @param f
     * @throws IOException
     */
    public static void write(String text, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);
        out.write(text.getBytes());
        out.close();
    }

    /**
     * @param in
     * @param f
     * @throws IOException
     */
    public static void write(InputStream in, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);

        int read = 0;
        byte[] buff = new byte[1024];
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        in.close();
        out.close();
    }
}

Related

  1. unZip(File zipFile, String extPlace, boolean reservZipFile)
  2. unzip(File zipFileName, File targetDir)
  3. unzip(File zipName, File destDir)
  4. unZip(File zipPath, File destPath)
  5. unzip(File zippedFile)
  6. unzip(File zippedFile, File targetDir)
  7. unzip(final byte[] zippedContent)
  8. unzip(final File dir, final ZipInputStream from)
  9. unzip(final File zip, final File dir)