Java Unzip File uncompressEveryFileFromDirectory(File srcPath, File dstPath)

Here you can find the source of uncompressEveryFileFromDirectory(File srcPath, File dstPath)

Description

Uncompress data to the destination directory.

License

Open Source License

Parameter

Parameter Description
srcZipPath path to the compressed file, could be the file or the directory
dstDirPath destination path

Exception

Parameter Description
IOException if any exception occurred

Declaration

public static void uncompressEveryFileFromDirectory(File srcPath, File dstPath) throws IOException 

Method Source Code

//package com.java2s;
/*//from w  w w .  j  ava 2 s .c  o m
 * Copyright (C) 2009 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

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

import java.util.zip.ZipInputStream;

public class Main {
    /**
     * Uncompress data to the destination directory. 
     * 
     * @param srcZipPath
     *          path to the compressed file, could be the file or the directory
     * @param dstDirPath
     *          destination path
     * @throws IOException
     *          if any exception occurred
     */
    public static void uncompressEveryFileFromDirectory(File srcPath, File dstPath) throws IOException {
        if (srcPath.isDirectory()) {
            if (!dstPath.exists()) {
                dstPath.mkdirs();
            }

            String files[] = srcPath.list();
            for (int i = 0; i < files.length; i++) {
                uncompressEveryFileFromDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
            }
        } else {
            ZipInputStream in = null;
            OutputStream out = null;

            try {
                in = new ZipInputStream(new FileInputStream(srcPath));
                in.getNextEntry();

                out = new FileOutputStream(dstPath);

                transfer(in, out);
            } finally {
                if (in != null) {
                    in.close();
                }

                if (out != null) {
                    out.close();
                }
            }
        }
    }

    /**
     * Transfer bytes from in to out
     */
    public static void transfer(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[2048];

        int len;

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }
}

Related

  1. uncompress(String inputCompressedFile, File outputFile)
  2. uncompress(String inputFile, String toDir)
  3. uncompress(String str)
  4. uncompress(ZipInputStream zipIn)
  5. uncompressDirectory(File zipSource, File target)
  6. unCompressGzipFile(String path)
  7. uncompressZipEntry(ZipInputStream zis, ZipEntry zipEntry, String dest)
  8. unzip(File aFile)
  9. unzip(File archive)