Java Unzip File uncompress(final String zipFileName, final String directory)

Here you can find the source of uncompress(final String zipFileName, final String directory)

Description

Uncompress a given zip file named zipFileName over directory named directory.

License

Open Source License

Parameter

Parameter Description
zipFileName The zip file to unzip.
directory The directory where to unzip the file.

Exception

Parameter Description
IOException Occurs if zip file can't be read properly or some writtingissues happen when creating the unzipped files on disk.

Declaration

public static void uncompress(final String zipFileName, final String directory) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2010 IBM Corporation.
 * 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  a  v a 2s  .c o  m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private final static int BUFFER_SIZE = 2048;

    /**
     * Uncompress a given zip file named <i>zipFileName</i> over directory named
     * <i>directory</i>. Note that files unzipped have a verification of
     * existence, and if the verdict is positive then no overwritting is realized.
     * 
     * @param zipFileName
     *            The zip file to unzip.
     * @param directory
     *            The directory where to unzip the file.
     * @throws IOException
     *             Occurs if zip file can't be read properly or some writting
     *             issues happen when creating the unzipped files on disk.
     */
    public static void uncompress(final String zipFileName, final String directory) throws IOException {
        final ZipInputStream zipIs = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));

        ZipEntry entry;
        int count;
        final byte data[] = new byte[BUFFER_SIZE];
        while ((entry = zipIs.getNextEntry()) != null) {
            final File file = new File(directory, entry.getName());
            if (meetRequirements(file)) {
                final BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
                while ((count = zipIs.read(data, 0 /* offset */, BUFFER_SIZE /* length */)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
        }
        zipIs.close();
    }

    private static boolean meetRequirements(final File file) {
        assert (file != null);
        final File parentFile = file.getParentFile();
        assert (parentFile != null);
        return (!file.exists()) && (parentFile.exists() || parentFile.mkdirs());
    }
}

Related

  1. decompressFile(String inName)
  2. decompressGzip(File src, File trgt)
  3. uncompress(ByteArrayInputStream bais)
  4. uncompress(File zipFile, File folder)
  5. uncompress(final InputStream inputStream)
  6. uncompress(String chemin, String tempPrefix)
  7. uncompress(String compressedValue)
  8. uncompress(String inputCompressedFile, File outputFile)
  9. uncompress(String inputFile, String toDir)