Java Unzip ZipEntry unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDirectory)

Here you can find the source of unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDirectory)

Description

unzip Entry

License

Open Source License

Declaration

private static void unzipEntry(ZipFile zipfile, ZipEntry entry,
            File outputDirectory) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2017 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v 1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributor:/*from ww  w .  j  av  a  2s .  co m*/
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

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

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.ZipFile;

public class Main {
    private static void unzipEntry(ZipFile zipfile, ZipEntry entry,
            File outputDirectory) {

        if (entry.isDirectory()) {
            createDirectory(new File(outputDirectory, entry.getName()));
            return;
        }

        File outputFile = new File(outputDirectory, entry.getName());
        if (!outputFile.getParentFile().exists()) {
            createDirectory(outputFile.getParentFile());
        }

        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(
                    zipfile.getInputStream(entry));
            outputStream = new BufferedOutputStream(new FileOutputStream(
                    outputFile));
            copy(inputStream, outputStream, 1024);
        } catch (IOException ex) {
        } finally {
            try {
                outputStream.close();
            } catch (Exception ex) {
            }
            try {
                inputStream.close();
            } catch (Exception ex) {
            }
        }
        outputFile.setExecutable(true);
        outputFile.setReadable(true);
        outputFile.setWritable(true);
    }

    public static void createDirectory(File directory) {
        if (!directory.exists()) {
            directory.mkdirs();
        }
    }

    private static void copy(InputStream inputStream,
            OutputStream outputStream, int bufferSize) throws IOException {
        byte[] buffer = new byte[bufferSize];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
    }
}

Related

  1. unzipEntry(ZipFile zipFile, ZipEntry entry, File entryTarget)
  2. unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir)
  3. unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir)
  4. unzipEntry(ZipInputStream zin, File f)
  5. unzipEntry(ZipInputStream zis, File newFile)
  6. unzipEntry(ZipInputStream zis, File out)
  7. unzipEntry(ZipInputStream zis, File targetFile)