Java Unzip File unzip(File targetZip, File dirToExtract)

Here you can find the source of unzip(File targetZip, File dirToExtract)

Description

unzip

License

Open Source License

Declaration

public static long unzip(File targetZip, File dirToExtract)
            throws Exception 

Method Source Code

//package com.java2s;
/*//  w  ww . ja  v a 2s.co  m
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.RandomAccessFile;

import java.util.Enumeration;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    final private static Logger logger = Logger
            .getLogger("com.gigaspaces.zip");

    public static long unzip(File targetZip, File dirToExtract)
            throws Exception {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Unzipping file [" + targetZip.getAbsolutePath()
                    + "] with size [" + targetZip.length() + "] to ["
                    + dirToExtract.getAbsolutePath() + "]");
        }
        long totalSize = 0;
        final int bufferSize = 4098;
        byte data[] = new byte[bufferSize];
        ZipFile zipFile = new ZipFile(targetZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            if (entry.isDirectory()) {
                File dir = new File(dirToExtract.getAbsolutePath() + "/"
                        + entry.getName().replace('\\', '/'));
                for (int i = 0; i < 5; i++) {
                    dir.mkdirs();
                }
            } else {
                BufferedInputStream is = new BufferedInputStream(
                        zipFile.getInputStream(entry));
                int count;
                File file = new File(dirToExtract.getAbsolutePath() + "/"
                        + entry.getName().replace('\\', '/'));
                if (file.getParentFile() != null) {
                    file.getParentFile().mkdirs();
                }
                if (logger.isLoggable(Level.FINEST)) {
                    logger.finest("Extracting zip entry ["
                            + entry.getName() + "] with size ["
                            + entry.getSize() + "] to ["
                            + file.getAbsolutePath() + "]");
                }
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                        bufferSize);
                while ((count = is.read(data, 0, bufferSize)) != -1) {
                    totalSize += count;
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();

                // sync the file to the file system
                RandomAccessFile ras = new RandomAccessFile(file, "rw");
                try {
                    ras.getFD().sync();
                } finally {
                    try {
                        ras.close();
                    } catch (Exception e1) {
                        // ignore
                    }
                }
            }
        }
        zipFile.close();
        return totalSize;
    }
}

Related

  1. unzip(File src, File dest)
  2. unzip(File src, File target)
  3. unzip(File srcZipFile, File tgtDir)
  4. unzip(File tarFile, File destinationDir)
  5. unzip(File target)
  6. unzip(File zip)
  7. unZip(File zip, File dest)
  8. unzip(File zip, File destination)
  9. unzip(File zip, File dir)