Java FileOutputStream Create writeFile(ZipEntry entry, ZipFile zipFile, File file)

Here you can find the source of writeFile(ZipEntry entry, ZipFile zipFile, File file)

Description

write File

License

Open Source License

Declaration

private static void writeFile(ZipEntry entry, ZipFile zipFile, File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 EclipseSource and others.
 * 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://w ww . j a  v  a  2  s  .  co m
 *    Holger Staudacher - initial API and implementation, ongoing development
 ******************************************************************************/

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;
import com.google.common.io.ByteStreams;

public class Main {
    private static void writeFile(ZipEntry entry, ZipFile zipFile, File file) throws IOException {
        if (!buildDirectory(file.getParentFile())) {
            throw new IOException("Could not create directory: " + file.getParentFile());
        }
        if (!entry.isDirectory()) {
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file)));
        } else {
            if (!buildDirectory(file)) {
                throw new IOException("Could not create directory: " + file);
            }
        }
    }

    private static boolean buildDirectory(File file) {
        return file.exists() || file.mkdirs();
    }

    private static void copyInputStream(InputStream in, OutputStream out) throws IOException {
        try {
            ByteStreams.copy(in, out);
        } finally {
            in.close();
            out.close();
        }
    }
}

Related

  1. writeFile(String filePathName, Object obj)
  2. writeFile(String p_filename, String p_buffer)
  3. writeFile(String path, byte[] contents)
  4. writeFile(String path, byte[] data)
  5. writeFile(String path, Properties store, String comment)
  6. writeFile0(File file, CharSequence content, Iterable lines)
  7. writeFileAsBytes(String fullPath, byte[] bytes)
  8. writeFileAsString(final File filePath, final String output, final String charset)
  9. writeFileAsString(String filename, String contents)