Java Zip Files addFileToZip(File file, String entryName, ZipOutputStream zos)

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

Description

Inserts a file into a zipstream using the specified entryName.

License

Apache License

Parameter

Parameter Description
file a parameter
entryName a parameter
zos a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static final void addFileToZip(File file, String entryName, ZipOutputStream zos) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**// ww  w.j  a v a 2s.  c  om
     * Inserts a file into a zipstream using the specified entryName.
     * 
     * @param file
     * @param entryName
     * @param zos
     * @throws IOException
     */
    public static final void addFileToZip(File file, String entryName, ZipOutputStream zos) throws IOException {
        byte[] buffer = new byte[8192];
        int read = 0;
        FileInputStream in = new FileInputStream(file);
        ZipEntry entry = new ZipEntry(entryName);
        zos.putNextEntry(entry);
        while (-1 != (read = in.read(buffer))) {
            zos.write(buffer, 0, read);
        }
        in.close();
    }

    /**
     * Inserts a stream into a zipstream using the specified entryName.
     * 
     * @param in
     * @param entryName
     * @param zos
     * @throws IOException
     */
    public static final void addFileToZip(InputStream in, String entryName, ZipOutputStream zos)
            throws IOException {
        byte[] buffer = new byte[8192];
        int read = 0;
        ZipEntry entry = new ZipEntry(entryName);
        zos.putNextEntry(entry);
        while (-1 != (read = in.read(buffer))) {
            zos.write(buffer, 0, read);
        }
        in.close();
    }
}

Related

  1. addFilesToZip(File zipFile, File[] files, String[] fileNames)
  2. addFileToZip(File file, String entryName, ZipOutputStream zOut)
  3. addFileToZip(File file, String parentFolderName, ZipOutputStream zip)
  4. addFileToZip(File file, ZipOutputStream zos)
  5. addFileToZip(File in, File parent, ZipOutputStream out)