Java Unzip ZipFile unzip(ZipFile zipFile, File dstDir)

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

Description

Unzips the given zip file to the given destination directory extracting only those entries the pass through the given filter.

License

Open Source License

Parameter

Parameter Description
zipFile the zip file to unzip
dstDir the destination directory

Exception

Parameter Description
IOException in case of problem

Declaration

public static void unzip(ZipFile zipFile, File dstDir) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * 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
 *
 * Contributors:/*from  w  ww.  j av a 2  s.  c  o  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    /**
     * Unzips the given zip file to the given destination directory extracting only those entries the pass through the
     * given filter.
     * 
     * @param zipFile
     *            the zip file to unzip
     * @param dstDir
     *            the destination directory
     * @throws IOException
     *             in case of problem
     */
    public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
        unzip(zipFile, dstDir, dstDir, 0);
    }

    private static void unzip(ZipFile zipFile, File rootDstDir, File dstDir, int depth) throws IOException {

        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        try {
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    continue;
                }
                String entryName = entry.getName();
                File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
                file.getParentFile().mkdirs();
                InputStream src = null;
                OutputStream dst = null;
                try {
                    src = zipFile.getInputStream(entry);
                    dst = new FileOutputStream(file);
                    transferData(src, dst);
                } finally {
                    if (dst != null) {
                        try {
                            dst.close();
                        } catch (IOException e) {
                            // don't need to catch this
                        }
                    }
                    if (src != null) {
                        try {
                            src.close();
                        } catch (IOException e) {
                            // don't need to catch this
                        }
                    }
                }
            }
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                // don't need to catch this
            }
        }
    }

    /**
     * Returns the given file path with its separator character changed from the given old separator to the given new
     * separator.
     * 
     * @param path
     *            a file path
     * @param oldSeparator
     *            a path separator character
     * @param newSeparator
     *            a path separator character
     * @return the file path with its separator character changed from the given old separator to the given new
     *         separator
     */
    public static String changeSeparator(String path, char oldSeparator, char newSeparator) {
        return path.replace(oldSeparator, newSeparator);
    }

    /**
     * Copies all bytes in the given source stream to the given destination stream. Neither streams are closed.
     * 
     * @param source
     *            the given source stream
     * @param destination
     *            the given destination stream
     * @throws IOException
     *             in case of error
     */
    private static void transferData(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }

    public static String read(File source) throws IOException {
        InputStream in = new FileInputStream(source);
        try {
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                sb.append(new String(buf, 0, len));
            }
            return sb.toString();
        } finally {
            in.close();
        }
    }

    public static void write(String fileName, StringBuffer content) throws IOException {
        Writer writer = new FileWriter(fileName);
        try {
            writer.write(content.toString());
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                // don't need to catch this
            }
        }
    }
}

Related

  1. unzip(ZipEntry entry, ZipFile zipfile, File explodedDir)
  2. unzip(ZipFile zip, File fileDir)
  3. unzip(ZipFile zipFile, File dest)
  4. unzip(ZipFile zipFile, File destDir)
  5. unzip(ZipFile zipFile, File destDirectory)
  6. unZip(ZipFile zipFile, File outputDir)