package com.xypower.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    public static interface Filter {
        boolean match(String file);
    }
    public static void ZipFolder(File srcFile, File zipFile, Filter filter) {
        try {
            ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
            ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter);
            outZip.finish();
            outZip.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam, Filter filter) throws Exception {
        if (zipOutputSteam == null)
            return;
        File file = new File(srcFileParentName + srcFileName);

        if (file.isFile()) {
            ZipEntry zipEntry = new ZipEntry(srcFileName);
            FileInputStream inputStream = new FileInputStream(file);
            zipOutputSteam.putNextEntry(zipEntry);
            int len;
            byte[] buffer = new byte[1024 * 256];
            while ((len = inputStream.read(buffer)) != -1) {
                zipOutputSteam.write(buffer, 0, len);
            }
            zipOutputSteam.closeEntry();
        } else {
            // folder
            String fileList[] = file.list();
            // NO sub file
            if (fileList.length <= 0) {
                ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator);
                zipOutputSteam.putNextEntry(zipEntry);
                zipOutputSteam.closeEntry();
            }
            //
            for (int i = 0; i < fileList.length; i++) {
                if (filter != null && !filter.match(fileList[i])) {
                    continue;
                }
                ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam, filter);
            }
        }
    }

    public static void ZipFiles(List<String> srcFiles, File zipFile) {
        try {
            ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
            // ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip);
            byte[] buffer = new byte[1024 * 256];

            for (String path : srcFiles) {
                File file = new File(path);
                if (!file.exists()) {
                    continue;
                }
                ZipEntry zipEntry = new ZipEntry(path);
                FileInputStream inputStream = new FileInputStream(file);
                outZip.putNextEntry(zipEntry);
                int len;

                while ((len = inputStream.read(buffer)) != -1) {
                    outZip.write(buffer, 0, len);
                }
                outZip.closeEntry();
            }

            outZip.finish();
            outZip.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
