package com.yh.saas.platform.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

public class GtAppendixUtil {

	private static Logger log = (Logger) LoggerFactory.getLogger(GtAppendixUtil.class);

	private GtAppendixUtil() {

	}

	/**
	 * 把接受的全部文件打成压缩包
	 * 
	 * @param List<File>;
	 * @param org.apache.tools.zip.ZipOutputStream
	 */
	public static void zipFiles(List<File> files, ZipOutputStream outputStream) {
		int size = files.size();
		for (int i = 0; i < size; i++) {
			File file = files.get(i);
			zipFile(file, outputStream);
		}
	}

	/**
	 * 根据输入的文件与输出流对文件进行打包
	 * 
	 * @param File
	 * @param org.apache.tools.zip.ZipOutputStream
	 */
	public static void zipFile(File inputFile, ZipOutputStream ouputStream) {
		try {
			if (inputFile.isFile()) {
				try (FileInputStream in = new FileInputStream(inputFile);
						BufferedInputStream bins = new BufferedInputStream(in, 512)) {
					ZipEntry entry = new ZipEntry(inputFile.getName());
					ouputStream.putNextEntry(entry);
					// 向压缩文件中输出数据
					int nNumber;
					byte[] buffer = new byte[512];
					while ((nNumber = bins.read(buffer)) != -1) {
						ouputStream.write(buffer, 0, nNumber);
					}
				}
			} else {
				File[] files = inputFile.listFiles();
				for (int i = 0; i < files.length; i++) {
					zipFile(files[i], ouputStream);
				}
			}
		} catch (Exception e) {
			log.error("文件打包出错", e);
		}
	}

	public static void downloadZip(File rootFile, String frontOrigin, HttpServletResponse response) {
		try {
			response.reset();

			response.setContentType("application/octet-stream");
			// 如果输出的是中文名的文件，在此处就要用URLEncoder.encode方法进行处理
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String(rootFile.getName().getBytes("UTF-8"), "ISO8859-1"));
			response.setHeader("Access-Control-Allow-Origin", frontOrigin);
			response.setHeader("Access-Control-Allow-Credentials", "true");
			// 以流的形式下载文件。
			try (InputStream fis = new BufferedInputStream(new FileInputStream(rootFile.getPath()));
					OutputStream toClient = new BufferedOutputStream(response.getOutputStream())) {
				byte[] buffer = new byte[fis.available()];
				int nNumber;
				while ((nNumber = fis.read(buffer)) > 0) {
					toClient.write(buffer, 0, nNumber);
				}
			}
		} catch (Exception ex) {
			log.error("文件下载出错", ex);
		} finally {
			try {
				File file = new File(rootFile.getPath());
				boolean result = file.delete();
				if (!result) {
					log.warn(file.getAbsolutePath() + "删除失败！");
				}
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
	}

	/**
	 * 计算文件大小
	 * 
	 * @param size
	 * @return
	 */
	public static String getFileSize(int size) {
		String result = "";
		if (size / 1024 / 1024 >= 1) {
			result = String.valueOf(size / 1024 / 1024) + "MB";
		} else if (size / 1024 >= 1) {
			result = String.valueOf(size / 1024) + "KB";
		} else {
			result = String.valueOf(size) + "B";
		}
		return result;
	}

}
