Java FileChannel Copy copyFromZip(ZipFile zipFile, String locationInBundle, File destination)

Here you can find the source of copyFromZip(ZipFile zipFile, String locationInBundle, File destination)

Description

copy From Zip

License

Open Source License

Declaration

private static void copyFromZip(ZipFile zipFile, String locationInBundle, File destination) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Red Hat, Inc./*  w w  w . ja  va 2 s  . co  m*/
 * 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
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.nio.channels.Channels;
import java.nio.channels.FileChannel;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    private static void copyFromZip(ZipFile zipFile, String locationInBundle, File destination) throws IOException {

        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (zipEntry.getName().startsWith(locationInBundle)) {

                String path = zipEntry.getName().substring(locationInBundle.length());
                File file = new File(destination, path);

                if (!zipEntry.isDirectory()) {
                    createFileFromZipFile(file, zipFile, zipEntry);
                } else {
                    if (!file.exists()) {
                        file.mkdir();
                    }
                }
            }
        }

    }

    private static void createFileFromZipFile(File file, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
        if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
            throw new IOException("Can not create parent directory for " + file.toString());
        }
        file.createNewFile();
        FileOutputStream fout = null;
        FileChannel out = null;
        InputStream in = null;
        try {
            fout = new FileOutputStream(file);
            out = fout.getChannel();
            in = zipFile.getInputStream(zipEntry);
            out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE);
        } finally {
            if (out != null)
                out.close();
            if (in != null)
                in.close();
            if (fout != null)
                fout.close();
        }
    }
}

Related

  1. copyFileUsingChannel(File source, File dest)
  2. copyFileUsingFileChannels(File source, File dest)
  3. copyFileUsingFileChannels(File source, File dest)
  4. copyFileUsingFileChannels(File source, File dest)
  5. copyFolder(File fin, File fout)
  6. copyInternal(FileInputStream in, FileOutputStream out)
  7. copyLarge(FileInputStream in, FileOutputStream out)
  8. copyNio(final File source_file, final File destination_file)
  9. copyRec(File src, File dst)