package com.blueland.bpm.manage.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ProcessZipUtil {
    
    private static final Log log = LogFactory.getLog(ProcessZipUtil.class);


	/**
	 * 压缩文件
	 * 
	 * @param srcfile
	 *            File[] 需要压缩的文件列表
	 * @param zipfile
	 *            File 压缩后的文件
	 */
	public static void zipFiles(Map<String, byte[]> srcFileBytes,
			OutputStream out) {
		// Create the ZIP file
		ZipOutputStream zipOut = new ZipOutputStream(out);
		try {
			// Compress the files
			for (Entry<String, byte[]> srcFileByte : srcFileBytes.entrySet()) {
				ByteArrayInputStream in = new ByteArrayInputStream(
						srcFileByte.getValue());
				try {
					// Add ZIP entry to output stream.
					zipOut.putNextEntry(new ZipEntry(srcFileByte.getKey()));
					// Transfer bytes from the file to the ZIP file
					IOUtils.copy(in, zipOut);
					// Complete the entry
					zipOut.closeEntry();
				} finally {
					IOUtils.closeQuietly(in);
				}
			}
		} catch (IOException e) {
			log.error("ProcessZipUtil zipFiles exception:" + e);
		} finally {
			//IOUtils.closeQuietly(zipOut);
		}
	}

	/**
	 * 解压缩
	 * 
	 * @param zipfile
	 *            File 需要解压缩的文件
	 * @param descDir
	 *            String 解压后的目标目录
	 */
	public static Map<String,byte[][]> unZipFiles(byte[] zipfile) {
		Map<String,byte[][]> processFile = new HashMap<String,byte[][]>();
		ZipInputStream zf = null;
		try {
			// Open the ZIP file
			ZipEntry entry = null;
			zf = new ZipInputStream(new ByteArrayInputStream(zipfile));
			while ((entry = zf.getNextEntry()) != null) {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				try {
					String zipEntryName = entry.getName();
					String[] zipEntryNames = new String[3];
					if (zipEntryName.indexOf(".") != -1) {
						zipEntryNames = zipEntryName.split("\\.");
					}
					IOUtils.copy(zf, out);
					if (processFile.containsKey(zipEntryNames[0])) {
						if (zipEntryNames[1].indexOf("data") != -1) {
							processFile.get(zipEntryNames[0])[1]=
									out.toByteArray();
						} else if (zipEntryNames[1].indexOf("bpmn20") != -1) {
							processFile.get(zipEntryNames[0])[0]=
									out.toByteArray();
						}
					} else {
						byte[][] values = new byte[2][];
						if (zipEntryNames[1].indexOf("data") != -1) {
							values[1]=out.toByteArray();
						} else if (zipEntryNames[1].indexOf("bpmn20") != -1) {
							values[0]=out.toByteArray();
						}
						processFile.put(zipEntryNames[0], values);
					}
					zf.closeEntry();
				} finally {
					IOUtils.closeQuietly(out);
				}
			}
		} catch (IOException e) {
			log.error("ProcessZipUtil unZipFiles exception:" + e);
		} finally {
			IOUtils.closeQuietly(zf);
		}
		return processFile;
	}

	/**
	 * Main
	 * 
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		FileInputStream input = new FileInputStream("d:\\项目报价工作量估算.xls");
		FileOutputStream out = new FileOutputStream("d:\\a.zip");
		// Create the ZIP file
		ZipOutputStream zipOut = new ZipOutputStream(out);
		try {
			try {
				// Add ZIP entry to output stream.
				zipOut.putNextEntry(new ZipEntry("d/项目报价工作量估算.xls"));
				// Transfer bytes from the file to the ZIP file
				IOUtils.copy(input, zipOut);
				// Complete the entry
				zipOut.closeEntry();
			} finally {
				IOUtils.closeQuietly(input);
			}

		} catch (IOException e) {
			log.error("ProcessZipUtil zipFiles exception:" + e);
		} finally {
			// IOUtils.closeQuietly(zipOut);
		}

	}
}