Java Unzip ZipFile unZip(ZipFile zipFile, File outputDir)

Here you can find the source of unZip(ZipFile zipFile, File outputDir)

Description

Unzips the zipfile in the output directory.

License

Open Source License

Parameter

Parameter Description
zipFile the zip file
outputDir full path of the output directory

Exception

Parameter Description

Declaration

public static void unZip(ZipFile zipFile, File outputDir) throws java.io.IOException 

Method Source Code


//package com.java2s;
/*==========================================================================*\
 |  $Id: FileUtilities.java,v 1.9 2011/05/27 15:30:56 stedwar2 Exp $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2006-2008 Virginia Tech
 |
 |  This file is part of Web-CAT.//w  w w .j a va2  s.c o  m
 |
 |  Web-CAT is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU Affero General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  Web-CAT is distributed in the hope that it will be useful,
 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.
 |
 |  You should have received a copy of the GNU Affero General Public License
 |  along with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class Main {
    static final private int BUFFER_SIZE = 65536;

    /**
     * Unzips the zipfile in the output directory.  This method is
     * provided purely for use by the Bootstrap code.  All other code
     * should use the org.webcat.archives.ArchiveManager
     * class instead.
     *
     * @param zipFile the zip file
     * @param outputDir full path of the output directory
     * @throws java.io.IOException if occurs during unzipping
     */
    public static void unZip(ZipFile zipFile, File outputDir) throws java.io.IOException {
        Enumeration<? extends ZipEntry> e = zipFile.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            File entryFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                entryFile.mkdirs();
            } else {
                File parent = entryFile.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(entry));
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(entryFile));
                copyStream(in, out);
                in.close();
                out.close();
                long modTime = entry.getTime();
                if (modTime != -1) {
                    entryFile.setLastModified(modTime);
                }
            }
        }
    }

    /**
     * Copies the contents of an input stream onto an existing output
     * stream.  The output stream is flushed when the operation
     * is complete.
     *
     * @param in  the input stream to copy
     * @param out the destination
     * @throws IOException if there are IO errors
     */
    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        // read in increments of BUFFER_SIZE
        byte[] b = new byte[BUFFER_SIZE];
        int count = in.read(b);
        while (count > -1) {
            out.write(b, 0, count);
            count = in.read(b);
        }
        out.flush();
    }
}

Related

  1. unzip(ZipFile zip, File fileDir)
  2. unzip(ZipFile zipFile, File dest)
  3. unzip(ZipFile zipFile, File destDir)
  4. unzip(ZipFile zipFile, File destDirectory)
  5. unzip(ZipFile zipFile, File dstDir)