Android Zip File Create ZipFiles(File zip, File... srcFiles)

Here you can find the source of ZipFiles(File zip, File... srcFiles)

Description

Zip Files

Declaration

public static void ZipFiles(File zip, File... srcFiles)
        throws IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

    public static void ZipFiles(File zip, File... srcFiles)
            throws IOException {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
        ZipFiles(out, "backup", srcFiles);
        out.close();/*from w  w  w  .j a  v a2 s.  c  om*/
    }

    private static void ZipFiles(ZipOutputStream out, String path,
            File... srcFiles) {
        path = path.replaceAll("\\*", "/");
        if (!path.endsWith("/")) {
            path += "/";
        }
        byte[] buf = new byte[1024];
        try {
            for (int i = 0; i < srcFiles.length; i++) {
                if (srcFiles[i].isDirectory()) {
                    File[] files = srcFiles[i].listFiles();
                    String srcPath = srcFiles[i].getName();
                    srcPath = srcPath.replaceAll("\\*", "/");
                    if (!srcPath.endsWith("/")) {
                        srcPath += "/";
                    }
                    out.putNextEntry(new ZipEntry(path + srcPath));
                    ZipFiles(out, path + srcPath, files);
                } else {
                    FileInputStream in = new FileInputStream(srcFiles[i]);
                    out.putNextEntry(new ZipEntry(path
                            + srcFiles[i].getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. zipFiles(Collection resFileList, File zipFile, String comment)
  2. zipFiles(File[] resFileList, File zipFile)
  3. zipFile(Collection resFileLIst, File zipFile)
  4. zipFile(Collection resFileLIst, File zipFile, String comment)
  5. zipIt(File zipFile, ArrayList files, Context context)
  6. createZip(String[] files, String zipFile)
  7. createZip(String[] files, String zipFile)
  8. compress(File file)
  9. compress(File file)