Java Zip File List zipFiles(ZipOutputStream out, String path, File... srcFiles)

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

Description

zip Files

License

Open Source License

Declaration

public static void zipFiles(ZipOutputStream out, String path, File... srcFiles) 

Method Source Code


//package com.java2s;
import java.io.*;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFiles(ZipOutputStream out, String path, File... srcFiles) {
        path = path.replaceAll("\\*", "/");
        if (!path.endsWith("/")) {
            path += "/";
        }//from www  .  j  av a2 s  .  c o m
        byte[] buf = new byte[1024];
        try {
            for (File srcFile : srcFiles) {
                if (srcFile.isDirectory()) {
                    File[] files = srcFile.listFiles();
                    String srcPath = srcFile.getName();
                    srcPath = srcPath.replaceAll("\\*", "/");
                    if (!srcPath.endsWith("/")) {
                        srcPath += "/";
                    }
                    out.putNextEntry(new ZipEntry(path + srcPath));
                    zipFiles(out, path + srcPath, files);
                } else {
                    try (FileInputStream in = new FileInputStream(srcFile)) {
                        out.putNextEntry(new ZipEntry(path + srcFile.getName()));
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        out.closeEntry();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. zipFiles(String filesPathToZip, String pathToSave)
  2. zipFiles(String output, String sDir, String sSearch)
  3. zipFiles(String output_dir, List files)
  4. zipFiles(String source, String target)
  5. zipFiles(String srcFolder, String destZipFile)
  6. zipFilesTo(Vector fileVector, String baseDir, File destFile)