package cn.com.qmth.stmms.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.poi.util.IOUtils;

/**
 * @author 熊开君
 * 
 */
public class ZIPUtil {

    private ZIPUtil() {
    }

    public void CreateZipFile(String filePath, String zipFilePath) {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(zipFilePath);
            zos = new ZipOutputStream(fos);
            // zos.setEncoding("GBK");
            writeZipFile(new File(filePath), zos, "");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null)
                    zos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void writeZipFile(File f, ZipOutputStream zos, String hiberarchy) {
        if (f.exists()) {
            if (f.isDirectory()) {
                hiberarchy += f.getName() + "/";
                File[] fif = f.listFiles();
                for (int i = 0; i < fif.length; i++) {
                    writeZipFile(fif[i], zos, hiberarchy);
                }

            } else {
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(f);
                    ZipEntry ze = new ZipEntry(hiberarchy + f.getName());
                    zos.putNextEntry(ze);
                    byte[] b = new byte[1024];
                    while (fis.read(b) != -1) {
                        zos.write(b);
                        b = new byte[1024];
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fis != null)
                            fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

    }

    // 压缩
    public static String compress(String source) {
        if (source == null || source.length() == 0) {
            return source;
        }
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            gzip.write(source.getBytes("ISO-8859-1"));
            gzip.close();
            return out.toString("ISO-8859-1");
        } catch (Exception e) {
            return source;
        }
    }

    // 解压缩
    public static String uncompress(String input) {
        if (input == null || input.length() == 0) {
            return input;
        }
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPInputStream gunzip = new GZIPInputStream(new ByteArrayInputStream(input.getBytes("ISO-8859-1")));
            IOUtils.copy(gunzip, out);
            return out.toString("ISO-8859-1");
        } catch (Exception e) {
            return input;
        }
    }

    private static ZIPUtil zu = null;

    public static ZIPUtil getInstance() {
        if (zu == null)
            zu = new ZIPUtil();
        return zu;

    }

}