package com.ym.mec.util.compress;

import java.io.BufferedOutputStream;
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;

/**
 * 工具类，GZIP解压缩工具 <br>
 * Copyright <a href="http://www.erayt.com/" target="_blank">erayt.com</a>(c)<br>
 * 
 * @author GuoZheyong
 * @version 2011-7-15
 */
public class GZipUtil {
	/**
	 * 数组初始长度
	 */
	private static final int BYTE_ARRAY_INITIAL_SIZE = 1024;

	/**
	 * 压缩
	 * 
	 * @param data
	 *            原始数据
	 * @return 压缩后数据
	 * @throws IOException
	 *             如果发生 I/O 错误
	 */
	public static byte[] gzipBytes(byte[] data) throws IOException {
		ByteArrayOutputStream baos = null;
		GZIPOutputStream gos = null;
		try {
			baos = new ByteArrayOutputStream(BYTE_ARRAY_INITIAL_SIZE);
			// 建立gzip压缩输出流
			gos = new GZIPOutputStream(baos);
			// 压缩数据
			gos.write(data);
			gos.finish();
			// 返回压缩字节流
			return baos.toByteArray();
		} finally {
			if (gos != null) {
				gos.close();
			}
			if (baos != null) {
				baos.close();
			}
		}
	}

	/**
	 * 解压
	 * 
	 * @param data
	 *            压缩数据
	 * @return 解压后数据
	 * @throws IOException
	 *             如果发生 I/O 错误
	 */
	public static byte[] gunzipBytes(byte[] data) throws IOException {
		if (data == null || data.length == 0) {
			return null;
		}
		ByteArrayInputStream bais = null;
		GZIPInputStream gis = null;
		ByteArrayOutputStream bos = null;
		try {
			bais = new ByteArrayInputStream(data);
			// 建立gzip压缩输入流
			gis = new GZIPInputStream(bais);
			// 返回解压字节流
			bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[BYTE_ARRAY_INITIAL_SIZE];
			int n = 0;
			while (-1 != (n = gis.read(buffer))) {
				bos.write(buffer, 0, n);
			}
			return bos.toByteArray();
		} finally {
			if (bos != null) {
				bos.close();
			}
			if (gis != null) {
				gis.close();
			}
			if (bais != null) {
				bais.close();
			}
		}
	}

	public static byte[] File2byte(String filePath) {
		byte[] buffer = null;
		try {
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	public static void byte2File(byte[] buf, String filePath, String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(filePath);
			if (!dir.exists() && dir.isDirectory()) {
				dir.mkdirs();
			}
			file = new File(filePath + File.separator + fileName);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(buf);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws Exception{
		byte[] fileBytes = File2byte("D:\\anypipe\\anypipe20150714.zip");
		byte[] gzipBytes = gunzipBytes(fileBytes);
		byte2File(gzipBytes,"D:\test","test");
	}

}
