Java Unzip File unzip(final File zipFile, final String targetDir)

Here you can find the source of unzip(final File zipFile, final String targetDir)

Description

unzip

License

Open Source License

Declaration

static public void unzip(final File zipFile, final String targetDir) 

Method Source Code

//package com.java2s;
/******************************************************************************
 *
 * CoreWall / Corelyzer - An Initial Core Description Tool
 * Copyright (C) 2004 - 2007 Arun Gangadhar Gudur Rao, Julian Yu-Chung Chen
 * Electronic Visualization Laboratory, University of Illinois at Chicago
 *
 * This software 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./*from  w w w . j av  a 2 s .  c  o m*/
 *
 * 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 Public License along
 * with this software; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Questions or comments about CoreWall should be directed to
 * cavern@evl.uic.edu
 *
 *****************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

public class Main {
    static public void unzip(final File zipFile, final String targetDir) {
        boolean MAC_OS_X = System.getProperty("os.name").toLowerCase().startsWith("mac os x");
        int buf_size = 2048;

        try {
            BufferedOutputStream dest; // = null;
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                System.out.println("-- Extracting: " + entry);

                int count;
                byte data[] = new byte[buf_size];

                // write the files to the disk
                String sp = System.getProperty("file.separator");
                String entryName = entry.getName();

                if (MAC_OS_X || System.getProperty("os.name").equalsIgnoreCase("linux")) {
                    entryName = entry.getName().replaceAll("\\\\|/", sp);
                }

                System.out.println("EntryName: " + entryName);
                String targetFileName = targetDir + sp + entryName;
                FileOutputStream fos = new FileOutputStream(targetFileName);
                dest = new BufferedOutputStream(fos, buf_size);
                while ((count = zis.read(data, 0, buf_size)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
            zis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. unzip(File zippedFile, File targetDir)
  2. unzip(final byte[] zippedContent)
  3. unzip(final File dir, final ZipInputStream from)
  4. unzip(final File zip, final File dir)
  5. unzip(final File zipFile, final File destDir)
  6. unzip(final InputStream is, final File outputDirectory)
  7. unzip(final InputStream zipFile, final File targetFolder)
  8. unZip(final String result)
  9. unzip(final String zipFile, final String outputFolder, final boolean createFolder)