Java Unzip File unzip(File tarFile, File destinationDir)

Here you can find the source of unzip(File tarFile, File destinationDir)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File tarFile, File destinationDir) throws IOException 

Method Source Code

//package com.java2s;
/*/*from www. j  av  a2s .com*/
 * NOTE: This copyright does *not* cover user programs that use HQ
 * program services by normal system calls through the application
 * program interfaces provided as part of the Hyperic Plug-in Development
 * Kit or the Hyperic Client Development Kit - this is merely considered
 * normal use of the program, and does *not* fall under the heading of
 * "derived work".
 * 
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of HQ.
 * 
 * HQ is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

import java.io.BufferedOutputStream;

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.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void unzip(File tarFile, File destinationDir) throws IOException {
        ZipInputStream zis = null;
        final int BUFFER = 2048;
        try {
            zis = new ZipInputStream(new FileInputStream(tarFile));
            // get the first entry in the archive
            ZipEntry zipEntry = zis.getNextEntry();

            while (zipEntry != null) {
                // create a file with the same name as the tarEntry
                File destPath = new File(destinationDir, zipEntry.getName());
                // create any parent directories
                File parent = destPath.getParentFile();
                if (parent != null) {
                    parent.mkdirs();
                }
                // if entry is directory, create it
                if (zipEntry.isDirectory()) {
                    destPath.mkdirs();
                } else {
                    int count;
                    byte data[] = new byte[BUFFER];
                    FileOutputStream fout = null;
                    BufferedOutputStream bos = null;
                    try {
                        // delete the file it already exists
                        if (destPath.exists())
                            destPath.delete();
                        fout = new FileOutputStream(destPath);
                        bos = new BufferedOutputStream(fout, BUFFER);
                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
                            bos.write(data, 0, count);
                        }
                        bos.flush();
                    } finally {
                        safeCloseStream(bos);
                    }
                }
                zipEntry = zis.getNextEntry();
            }
        } finally {
            safeCloseStream(zis);
        }
    }

    public static void safeCloseStream(InputStream in) {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // just swallow it
            }
        }
    }

    public static void safeCloseStream(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // just swallow it
            }
        }
    }
}

Related

  1. unzip(File sourceFile, File rootDir)
  2. unzip(File sourceZipfile, File directory)
  3. unzip(File src, File dest)
  4. unzip(File src, File target)
  5. unzip(File srcZipFile, File tgtDir)
  6. unzip(File target)
  7. unzip(File targetZip, File dirToExtract)
  8. unzip(File zip)
  9. unZip(File zip, File dest)