Java Zip Directory zipDirectories(List directories, File baseDirectory, File zipFile)

Here you can find the source of zipDirectories(List directories, File baseDirectory, File zipFile)

Description

zip Directories

License

Open Source License

Declaration

public static void zipDirectories(List<File> directories,
            File baseDirectory, File zipFile) throws IOException 

Method Source Code

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

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class Main {
    public static void zipDirectories(List<File> directories,
            File baseDirectory, File zipFile) throws IOException {
        zipDirectories(directories, baseDirectory, new FileOutputStream(
                zipFile), null);// www. ja  va2s . c o  m
    }

    public static void zipDirectories(List<File> directories,
            File baseDirectory, OutputStream out, String nameFilter)
            throws IOException {
        String basePath = baseDirectory.getAbsolutePath();

        List<String> fileList = new ArrayList<>();
        for (File directory : directories) {
            generateFileList(directory, fileList);
        }

        byte[] buffer = new byte[1024];

        ZipOutputStream zos = new ZipOutputStream(out);

        for (String file : fileList) {

            if (!file.startsWith(basePath))
                throw new IOException("Base path '" + basePath
                        + "' not found in '" + file + "'");

            if (nameFilter != null
                    && new File(file).getName().matches(nameFilter))
                continue;

            String zipFile = file.substring(basePath.length() + 1,
                    file.length());

            //System.out.println(zipFile);
            ZipEntry ze = new ZipEntry(zipFile);
            zos.putNextEntry(ze);

            FileInputStream in = new FileInputStream(file);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
        }

        zos.closeEntry();
        zos.close();
    }

    private static void generateFileList(File node, List<String> fileList) {

        if (node.isFile()) {
            fileList.add(node.getAbsolutePath());
        }

        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                generateFileList(new File(node, filename), fileList);
            }
        }

    }
}

Related

  1. zip(File directory, File base, ZipOutputStream zos)
  2. zip(File directory, File base, ZipOutputStream zos, byte[] buffer)
  3. zip(File directory, File file)
  4. zip(File directory, File zipFile)
  5. zip(File directory, File zipFile)
  6. zipDirectory(File baseDirectory, File output)
  7. zipDirectory(File dir, boolean includeDirInZip, boolean includeHidden)
  8. zipDirectory(File dir, File zipFile)
  9. zipDirectory(File dir, String base, ZipOutputStream zout)