Java Zip Files addFileToZip(File file, ZipOutputStream zos)

Here you can find the source of addFileToZip(File file, ZipOutputStream zos)

Description

Adds a file to the current zip output stream

License

Apache License

Parameter

Parameter Description
file the file to be added
zos the current zip output stream

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

private static void addFileToZip(File file, ZipOutputStream zos)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**//  w  w  w.  j a  v  a 2  s .  c om
 * Copyright 2016 SmartBear Software
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * A constants for buffer size used to read/write data
     */
    private static final int BUFFER_SIZE = 4096;

    /**
     * Adds a file to the current zip output stream
     * @param file the file to be added
     * @param zos the current zip output stream
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void addFileToZip(File file, ZipOutputStream zos)
            throws FileNotFoundException, IOException {
        zos.putNextEntry(new ZipEntry(file.getName()));

        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(file));

        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;

        while ((read = bis.read(bytesIn)) != -1) {
            zos.write(bytesIn, 0, read);
        }

        zos.closeEntry();
    }
}

Related

  1. addFilesToZip(File zipFile, File[] files, String[] fileNames)
  2. addFileToZip(File file, String entryName, ZipOutputStream zos)
  3. addFileToZip(File file, String entryName, ZipOutputStream zOut)
  4. addFileToZip(File file, String parentFolderName, ZipOutputStream zip)
  5. addFileToZip(File in, File parent, ZipOutputStream out)
  6. addFileToZip(File root, File file, ZipOutputStream zos)
  7. addFileToZip(final File file, final String zipName, final ZipOutputStream zipout)
  8. addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)