package com.xugame.gameconsole.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

	public static String unzipRecordFile(String zipFile, String unzipToPath)
			throws Exception {
		
		String mTempRecRepFile = "";
		
		ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile));
		ZipEntry zipEntry;
		String szName = "";

		while ((zipEntry = inZip.getNextEntry()) != null) {

			szName = zipEntry.getName();
			if (zipEntry.isDirectory()) {
				szName = szName.substring(0, szName.length() - 1);
				File folder = new File(unzipToPath + File.separator + szName);
				folder.mkdirs();
			} else {
				//String filePath;
				//filePath = unzipToPath + File.separator + szName;
				mTempRecRepFile = unzipToPath + File.separator + "tempReplay.record";
				File file = new File(mTempRecRepFile);
				if(file.exists()) file.delete();
				//此处需要判断文件夹是否存在，不存在则需要创建
				file.createNewFile();
				FileOutputStream out = new FileOutputStream(file);
				int len;
				final int BUFFERLEN = 1024*2;
				byte[] buffer = new byte[BUFFERLEN];
				while ((len = inZip.read(buffer)) != -1) {
					out.write(buffer, 0, len);
					out.flush();
				}
				try {
					inZip.closeEntry();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				out.flush();
				out.close();
			}
		}
		inZip.close();
		return mTempRecRepFile;
	}

	public static void unzipFile(String zipFile, String unzipToPath)
			throws Exception {

		ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile));
		ZipEntry zipEntry;
		String szName = "";

		while ((zipEntry = inZip.getNextEntry()) != null) {

			szName = zipEntry.getName();
			if (zipEntry.isDirectory()) {
				szName = szName.substring(0, szName.length() - 1);
				File folder = new File(unzipToPath + File.separator + szName);
				folder.mkdirs();
			} else {
				String filePath;
				filePath = unzipToPath + File.separator + szName;
				File file = new File(filePath);
				//此处需要判断文件夹是否存在，不存在则需要创建
				file.createNewFile();
				FileOutputStream out = new FileOutputStream(file);
				int len;
				final int BUFFERLEN = 1024*2;
				byte[] buffer = new byte[BUFFERLEN];
				while ((len = inZip.read(buffer)) != -1) {
					out.write(buffer, 0, len);
					out.flush();
				}
				try {
					inZip.closeEntry();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				out.flush();
				out.close();
			}
		}
		inZip.close();
	}
	
	public static void zipFolder(String srcFilePath, String zipFilePath)
			throws Exception {
		// 创建Zip包
		ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(
				zipFilePath));
		File file = new File(srcFilePath);
		zipFiles(file.getParent() + File.separator, file.getName(), outZip);
		outZip.finish();
		outZip.close();
	}
	private static void zipFiles(String folderPath, String filePath,
                                 ZipOutputStream zipOut) throws Exception {

		if (zipOut == null) {
			return;
		}
		File file = new File(folderPath + filePath);
		if (file.isFile()) {
			ZipEntry zipEntry = new ZipEntry(filePath);
			FileInputStream inputStream = new FileInputStream(file);
			zipOut.putNextEntry(zipEntry);
			int len;
			final int BUFFERLEN = 1024*2;
			byte[] buffer = new byte[BUFFERLEN];
			while ((len = inputStream.read(buffer)) != -1) {
				zipOut.write(buffer, 0, len);
			}
			inputStream.close();
			zipOut.closeEntry();
		} else {
			// 文件夹的方式,获取文件夹下的子文件
			String fileList[] = file.list();
			// 添加文件夹本身
			ZipEntry zipEntry = new ZipEntry(filePath + File.separator);
			zipOut.putNextEntry(zipEntry);
			zipOut.closeEntry();
			// 如果有子文件, 遍历子文件
			for (int i = 0; i < fileList.length; i++) {
				zipFiles(folderPath, filePath + File.separator + fileList[i],
						zipOut);
			}
		}
	}
}
