Here you can find the source of zipIt(File zipFile, ArrayList
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 |
Parameter | Description |
---|---|
IOException | IOException Thrown if something goes wrong with zipping and reading |
private static void zipIt(File zipFile, ArrayList<File> files, Context context) throws IOException
//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(); } }