Android Zip File Create zipFiles(File[] resFileList, File zipFile)

Here you can find the source of zipFiles(File[] resFileList, File zipFile)

Description

zip Files

Declaration

public static void zipFiles(File[] resFileList, File zipFile)
            throws IOException 

Method Source Code

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Collection;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUFF_SIZE = 1024 * 1024;

    public static void zipFiles(Collection<File> resFileList, File zipFile)
            throws IOException {
        if (!zipFile.exists()) {
            zipFile.createNewFile();/*from  w  w  w.java2  s. c o  m*/
        }
        ZipOutputStream zipout = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(zipFile),
                        BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.close();
    }

    public static void zipFiles(File[] resFileList, File zipFile)
            throws IOException {
        if (!zipFile.exists()) {
            zipFile.createNewFile();
        }
        ZipOutputStream zipout = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(zipFile),
                        BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.close();
    }

    public static void zipFiles(Collection<File> resFileList, File zipFile,
            String comment) throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(zipFile),
                        BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.setComment(comment);
        zipout.close();
    }

    public static void zipFile(File resFile, ZipOutputStream zipout,
            String rootpath) throws FileNotFoundException, IOException {
        rootpath = rootpath
                + (rootpath.trim().length() == 0 ? "" : File.separator)
                + resFile.getName();
        rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
        if (resFile.isDirectory()) {
            File[] fileList = resFile.listFiles();
            for (File file : fileList) {
                zipFile(file, zipout, rootpath);
            }
        } else {
            byte buffer[] = new byte[BUFF_SIZE];
            BufferedInputStream in = new BufferedInputStream(
                    new FileInputStream(resFile), BUFF_SIZE);
            zipout.putNextEntry(new ZipEntry(rootpath));
            int realLength;
            while ((realLength = in.read(buffer)) != -1) {
                zipout.write(buffer, 0, realLength);
            }
            in.close();
            zipout.flush();
            zipout.closeEntry();
            // /resFile.delete();
        }
    }
}

Related

  1. zipFiles(Collection resFileList, File zipFile, String comment)
  2. zipFiles(Collection resFileList, File zipFile, String comment)
  3. zipFiles(Collection resFileList, File zipFile, String comment)
  4. zipFiles(Collection resFileList, File zipFile, String comment)
  5. zipFiles(Collection resFileList, File zipFile, String comment)
  6. zipFile(Collection resFileLIst, File zipFile)
  7. zipFile(Collection resFileLIst, File zipFile, String comment)
  8. zipIt(File zipFile, ArrayList files, Context context)
  9. ZipFiles(File zip, File... srcFiles)