Java FileInputStream Copy copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file, final int bytesInOfZip)

Here you can find the source of copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file, final int bytesInOfZip)

Description

copy File To Zip

License

Open Source License

Declaration

private static int copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file,
            final int bytesInOfZip) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**/*from www.  j a v a 2  s. c  om*/
 * Copyright (C) 2015 BonitaSoft S.A.
 * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/

import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Scanner;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public static final String FILE_ENCODING = "UTF-8";

    private static int copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file,
            final int bytesInOfZip) throws FileNotFoundException, IOException {
        final FileInputStream fis = new FileInputStream(file);
        int bytesIn = bytesInOfZip;
        try {
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zos.write(readBuffer, 0, bytesIn);
            }
        } finally {
            fis.close();
        }
        return bytesIn;
    }

    /**
     * Read the contents from the given FileInputStream. Return the result as a String.
     *
     * @param inputStream
     *        the stream to read from
     * @return the content read from the inputStream, as a String
     */
    public static String read(final InputStream inputStream) {
        if (inputStream == null) {
            throw new IllegalArgumentException("Input stream is null");
        }
        Scanner scanner = null;
        try {
            scanner = new Scanner(inputStream, FILE_ENCODING);
            return read(scanner);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }

    private static String read(final Scanner scanner) {
        final StringBuilder text = new StringBuilder();
        boolean isFirst = true;
        while (scanner.hasNextLine()) {
            if (isFirst) {
                text.append(scanner.nextLine());
            } else {
                text.append(LINE_SEPARATOR + scanner.nextLine());
            }
            isFirst = false;
        }
        return text.toString();
    }

    /**
     * Read the contents of the given file.
     * 
     * @param file
     */
    public static String read(final File file) throws IOException {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            return read(fileInputStream);
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        }
    }

    public static void write(final File file, final byte[] content) throws FileNotFoundException, IOException {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(content);
            bos.flush();
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Related

  1. copyFileToOutputStream(String fileLocation, OutputStream os)
  2. copyFileToStream(File currentFile, OutputStream outputStream)
  3. copyFileToStream(File file, OutputStream stream)
  4. copyFileToStream(File file, OutputStream stream)
  5. copyFileToString(java.io.File f)
  6. copyFileUsingFileStreams(final File source, final File dest)
  7. copyFileUsingStream(File source, File dest)
  8. copyFileUsingStream(String source_path, String dest_path)