Android ZipOutputStream Write ZipFiles(ZipOutputStream out, String path, File... srcFiles)

Here you can find the source of ZipFiles(ZipOutputStream out, String path, File... srcFiles)

Description

Zip Files

Declaration

private static void ZipFiles(ZipOutputStream out, String path,
            File... srcFiles) 

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 .  ja v a2  s .  c o m*/
    }

    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. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  2. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  3. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  4. zipFile(ZipOutputStream out, File file)
  5. zipFile(ZipOutputStream out, File file)