package com.dev.swms.server.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;




/**
 * ZIP文件压缩和解压(要使用apache ant.jar以处理中文乱码)
 * 
 * @author XQM
 * @version 2.0
 */
public class ZipUtils {

	/**
	 * 可以处理中文文件名
	 */
	private static final int buffer = 2048;

	public static void main(String[] args) {
		unZip("D:\\test\\Pictures.zip");
	}

	public static List<String> unZip(String path) {
		int count = -1;
		int index = -1;
		String savepath = "";
		boolean flag = false;
		List<String> list= new ArrayList<String>();

		File file = null;
		InputStream is = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;

		savepath = path.substring(0, path.lastIndexOf("\\")) + "\\";

		try {
			ZipFile zipFile = new ZipFile(path);

			Enumeration<?> entries = zipFile.entries();

			while (entries.hasMoreElements()) {
				byte buf[] = new byte[buffer];

				ZipEntry entry = (ZipEntry) entries.nextElement();

				String filename = entry.getName();
				index = filename.lastIndexOf("/");
				if (index > -1)
					filename = filename.substring(index + 1);

				filename = savepath + filename;

				flag = isPics(filename);
				if (!flag)
					continue;

				file = new File(filename);
				file.createNewFile();

				is = zipFile.getInputStream(entry);

				fos = new FileOutputStream(file);
				bos = new BufferedOutputStream(fos, buffer);

				while ((count = is.read(buf)) > -1) {
					bos.write(buf, 0, count);
				}

				fos.close();

				is.close();
				list.add(filename);
			}

			zipFile.close();

		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return list;
	}

	public static boolean isPics(String filename) {
		boolean flag = false;

		if (filename.endsWith(".jpg") || filename.endsWith(".gif")
				|| filename.endsWith(".bmp") || filename.endsWith(".png")||filename.endsWith(".JPG") || filename.endsWith(".GIF")
				|| filename.endsWith(".BMP") || filename.endsWith(".PNG")
				|| filename.endsWith(".pdf") || filename.endsWith(".PDF"))
			flag = true;

		return flag;
	}
	
	 public static void zip(File file,String fileName) throws Exception{
//       File f = new File("a.jpg");
       FileInputStream fis = new FileInputStream(file);
       BufferedInputStream bis = new BufferedInputStream(fis);
       byte[] buf = new byte[1024];
       int len;
       FileOutputStream fos = new FileOutputStream(fileName.substring(0, fileName.length()-4)+".zip");
       BufferedOutputStream bos = new BufferedOutputStream(fos);
       ZipOutputStream zos = new ZipOutputStream(bos);//压缩包
       ZipEntry ze = new ZipEntry(file.getName());//这是压缩包名里的文件名
       zos.putNextEntry(ze);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处
       while((len=bis.read(buf))!=-1)
       {
          zos.write(buf,0,len);
          zos.flush();
       }
       bis.close();
       zos.close(); 
     }

}