Java Zip Files addFileToZip(ZipOutputStream out, InputStream in, String entry)

Here you can find the source of addFileToZip(ZipOutputStream out, InputStream in, String entry)

Description

Adds a file into a zip archive.

License

Open Source License

Parameter

Parameter Description
out the zip file
in the input stream to add into the zip
entry the entry for the file to add into the zip (not null)

Declaration

private static void addFileToZip(ZipOutputStream out, InputStream in,
        String entry) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.InputStream;

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

public class Main {
    /**// w w  w .  j a  v  a2s .co m
     * Adds a file into a zip archive.
     * 
     * @param out the zip file
     * @param in the input stream to add into the zip
     * @param entry the entry for the file to add into the zip (not null)
     */
    private static void addFileToZip(ZipOutputStream out, InputStream in,
            String entry) {

        byte[] buf = new byte[1024]; // Create a buffer for reading the files
        ZipEntry ze = new ZipEntry(entry);
        try {
            out.putNextEntry(ze);
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.closeEntry();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. addFileToZip(int skipprefix, File file, ZipOutputStream zipper)
  2. addFileToZip(String path, File srcFile, ZipOutputStream zip, boolean flag)
  3. addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
  4. addFileToZip(String path, String srcFile, ZipOutputStream zip)
  5. addFileToZip(String path, String srcFile, ZipOutputStream zip)
  6. addFileToZip(ZipOutputStream zipOutputStream, File file, String basePath)
  7. addFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)
  8. addFileToZip(ZipOutputStream zos, File file, File rootDir)
  9. addFileToZipOutputStream(File file, ZipOutputStream zipOs)