Android Zip Unzip File zip(File[] _files, File _zipFile)

Here you can find the source of zip(File[] _files, File _zipFile)

Description

zip

License

Open Source License

Declaration

public static boolean zip(File[] _files, File _zipFile) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUFFER = 2048;

    public static boolean zip(File[] _files, File _zipFile) {
        try {/*w w w.  jav  a  2 s .c om*/
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(_zipFile);

            ZipOutputStream out = new ZipOutputStream(
                    new BufferedOutputStream(dest));

            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.length; i++) {
                Log.d("Compress", "Adding: " + _files[i]);
                FileInputStream fi = new FileInputStream(_files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(_files[i].getName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Related

  1. zip(String[] files, String zipFile)
  2. zip(String[] files, String zipFile)
  3. zip(String[] files, String zipFile)
  4. zip(String[] files, String zipFile)
  5. zip(File srcFile, File targetFile)
  6. zip(List listFiles, String destZipFilePath)
  7. zipFile(File source, String basePath, ZipOutputStream zos)
  8. zipFileAtPath(File sourceFile, String toLocation)
  9. decompress(File file)