Java Zip File zipFile(File root, String outFileFullName, String[] suffixs, String[] nameMatche, String[] nameNotMatche)

Here you can find the source of zipFile(File root, String outFileFullName, String[] suffixs, String[] nameMatche, String[] nameNotMatche)

Description

zip File

License

Apache License

Declaration

public static void zipFile(File root, String outFileFullName, String[] suffixs, String[] nameMatche,
            String[] nameNotMatche) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.zip.Deflater;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static int BUFFER = 1024;

    public static void zipFile(File root, String outFileFullName, String[] suffixs, String[] nameMatche,
            String[] nameNotMatche) throws Exception {
        zipFile0(root, outFileFullName, getFileList(root, suffixs, nameMatche, nameNotMatche));
    }/*from   ww w.j  a  v  a 2  s.  co  m*/

    public static void zipFile(File root, String outFileFullName, List<File> fileList) throws Exception {
        zipFile0(root, outFileFullName, fileList);
    }

    private static void zipFile0(File root, String outFileFullName, List<File> fileList) throws Exception {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFileFullName));
        zos.setLevel(Deflater.BEST_COMPRESSION);
        ZipEntry ze = null;
        byte[] buf = new byte[BUFFER];
        int readLen = 0;
        for (int i = 0; i < fileList.size(); i++) {
            File f = fileList.get(i);
            ze = new ZipEntry(getAbsFileName(root, f));
            ze.setSize(f.length());
            ze.setTime(f.lastModified());
            zos.putNextEntry(ze);
            InputStream is = new BufferedInputStream(new FileInputStream(f));
            while ((readLen = is.read(buf, 0, BUFFER)) != -1) {
                zos.write(buf, 0, readLen);
            }
            is.close();
        }
        zos.close();
    }

    public static List<File> getFileList(File file, String[] suffixs) {
        return getFileList(file, new ArrayList<File>(), suffixs, null, null);
    }

    public static List<File> getFileList(File file, String[] suffixs, String[] nameMatche, String[] nameNotMatche) {
        return getFileList(file, new ArrayList<File>(), suffixs, nameMatche, nameNotMatche);
    }

    public static List<File> getFileList(File file, List<File> fileList, String[] suffixs) {
        return getFileList(file, fileList, suffixs, null, null);
    }

    public static List<File> getFileList(File file, List<File> fileList, String[] suffixs, String[] nameMatche,
            String[] nameNotMatche) {
        if (file.getAbsolutePath().contains(".svn"))
            return fileList;
        if (file.isFile()) {
            String filename = file.getName();
            boolean isNameRight = false;
            if (nameMatche != null && nameMatche.length > 0) {
                for (String str : nameMatche) {
                    if (filename.toLowerCase().contains(str.toLowerCase())) {
                        isNameRight = true;
                        break;
                    }
                }
            } else {
                isNameRight = true;
            }
            if (isNameRight) {
                if (nameNotMatche != null && nameNotMatche.length > 0) {
                    for (String str : nameNotMatche) {
                        if (filename.toLowerCase().contains(str.toLowerCase())) {
                            isNameRight = false;
                            break;
                        }
                    }
                }
            }
            if (isNameRight) {
                if (suffixs == null || suffixs.length == 0) {
                    fileList.add(file);
                } else {
                    int index = filename.lastIndexOf(".");
                    if (index != -1) {
                        boolean isright = false;
                        String suffix = filename.substring(index, filename.length());
                        for (String str : suffixs) {
                            if (suffix.equalsIgnoreCase(str)) {
                                isright = true;
                                break;
                            }
                        }
                        if (isright) {
                            fileList.add(file);
                        }
                    }
                }
            }
        } else if (file.isDirectory()) {
            File[] fs = file.listFiles();
            for (File f : fs) {
                getFileList(f, fileList, suffixs, nameMatche, nameNotMatche);
            }
        }
        return fileList;
    }

    private static String getAbsFileName(File root, File realFileName) {
        File real = realFileName;
        String ret = real.getName();
        while (true) {
            real = real.getParentFile();
            if (real == null)
                break;
            if (real.equals(root))
                break;
            else
                ret = real.getName() + "/" + ret;
        }
        return ret;
    }
}

Related

  1. zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)
  2. zipFile(File input, File output)
  3. zipFile(File inputFile, File outputZip)
  4. zipFile(File inputFile, String zipFilePath)
  5. zipFile(File resFile, ZipOutputStream zipout, String rootpath)
  6. zipFile(File source, File dest)
  7. zipFile(File source, File target)
  8. zipFile(File source, File target)
  9. zipFile(File source, String basePath, ZipOutputStream zos)