Android ZipOutputStream Write zipFile(File file, ZipOutputStream zos)

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

Description

Adds a file to the current zip output stream

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 zipFile(File file, ZipOutputStream zos)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
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 {
    /**/*from w w  w . ja  va 2  s  . c om*/
     * Size of the buffer 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 zipFile(File file, ZipOutputStream zos)
            throws FileNotFoundException, IOException {
        zos.putNextEntry(new ZipEntry(file.getName()));
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            long bytesRead = 0;
            byte[] bytesIn = new byte[BUFFER_SIZE];
            int read = 0;
            while ((read = bis.read(bytesIn)) != -1) {
                zos.write(bytesIn, 0, read);
                bytesRead += read;
            }
            zos.closeEntry();
        } finally {
            bis.close();
        }
    }
}

Related

  1. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  2. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  3. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  4. zipFile(File resFile, ZipOutputStream zipout, String rootpath)