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

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

Description

unzip Entry

License

Open Source License

Declaration

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2009 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 v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributor:/*from ww w.  j  a  v  a  2 s .  com*/
 *     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 outputDir) throws IOException {

        if (entry.isDirectory()) {
            createDir(new File(outputDir, entry.getName()));
            return;
        }

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

        BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

        try {
            copy(inputStream, outputStream);
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    private static void createDir(File dir) {
        if (dir.exists()) {
            deleteDirectory(dir);
        }
        if (!dir.mkdirs())
            throw new RuntimeException("Can not create dir " + dir);
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    static private boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }
}

Related

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