Java Zip File List zipFiles(String output, String sDir, String sSearch)

Here you can find the source of zipFiles(String output, String sDir, String sSearch)

Description

zip Files

License

Open Source License

Declaration

public static void zipFiles(String output, String sDir, String sSearch) throws Exception 

Method Source Code


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

import java.util.zip.*;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void zipFiles(String output, String sDir, String sSearch) throws Exception {

        File dir = new File(sDir).getCanonicalFile();
        ;/* w  w w. j  a  va 2 s  .c o  m*/
        File[] files = dir.listFiles();

        List<File> lst = new ArrayList<File>();

        for (File f : files) {
            if (sSearch.equals("*")) {
                lst.add(f);
            } else if (sSearch.indexOf("*.") >= 0) {
                if (f.getName().indexOf(sSearch.substring(sSearch.indexOf(".") + 1)) >= 1) {
                    lst.add(f);
                }
            }
        }

        if (lst.size() > 0) {
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
            zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
            for (File f : lst) {
                zipFile(zipOut, sDir, f);
            }
            zipOut.close();
        }
    }

    public static void zipFile(ZipOutputStream zipOut, String path, File file) throws IOException {

        if (!file.canRead()) {
            System.out.println("Cannot read " + file.getCanonicalPath() + " (maybe because of permissions)");
            return;
        }

        System.out.println("Compressing " + file.getName());
        zipOut.putNextEntry(new ZipEntry(file.getName()));

        FileInputStream fis = new FileInputStream(file);

        byte[] buffer = new byte[4092];
        int byteCount = 0;
        while ((byteCount = fis.read(buffer)) != -1) {
            zipOut.write(buffer, 0, byteCount);
            System.out.print('.');
            System.out.flush();
        }
        System.out.println();

        fis.close();
        zipOut.closeEntry();
    }
}

Related

  1. zipFiles(List srcfile, File zipfile)
  2. ZipFiles(String destination, Vector files)
  3. zipFiles(String filename, String[] files)
  4. zipFiles(String files[], String fielPath)
  5. zipFiles(String filesPathToZip, String pathToSave)
  6. zipFiles(String output_dir, List files)
  7. zipFiles(String source, String target)
  8. zipFiles(String srcFolder, String destZipFile)
  9. zipFiles(ZipOutputStream out, String path, File... srcFiles)