Android Zip File Create zipIt(File zipFile, ArrayList files, Context context)

Here you can find the source of zipIt(File zipFile, ArrayList files, Context context)

Description

Zips all files specified in an ArrayList into a given file

Parameter

Parameter Description
zipFile The file to zip files into
files The files to be zipped
context The application context, for getting files and the like

Exception

Parameter Description
IOException IOException Thrown if something goes wrong with zipping and reading

Declaration

private static void zipIt(File zipFile, ArrayList<File> files,
        Context context) throws IOException 

Method Source Code

//package com.java2s;
import android.content.Context;

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

import java.util.ArrayList;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**/*from   w ww.ja va 2s  . co  m*/
     * Zips all files specified in an ArrayList into a given file
     *
     * @param zipFile The file to zip files into
     * @param files   The files to be zipped
     * @param context The application context, for getting files and the like
     * @throws IOException IOException Thrown if something goes wrong with zipping and reading
     */
    private static void zipIt(File zipFile, ArrayList<File> files,
            Context context) throws IOException {

        byte[] buffer = new byte[1024];

        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        assert context.getFilesDir() != null;
        for (File file : files) {
            ZipEntry ze = new ZipEntry(file.getName());
            zos.putNextEntry(ze);

            FileInputStream in = new FileInputStream(file);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            in.close();
        }

        zos.closeEntry();
        zos.close();
    }
}

Related

  1. zipFiles(Collection resFileList, File zipFile, String comment)
  2. zipFiles(Collection resFileList, File zipFile, String comment)
  3. zipFiles(File[] resFileList, File zipFile)
  4. zipFile(Collection resFileLIst, File zipFile)
  5. zipFile(Collection resFileLIst, File zipFile, String comment)
  6. ZipFiles(File zip, File... srcFiles)
  7. createZip(String[] files, String zipFile)
  8. createZip(String[] files, String zipFile)
  9. compress(File file)