/**
 * 
 */
package com.inkasso.factoring.code.util;

import java.io.File;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;


public class CompressUtil {
	/**
	 * 压缩一个指定的目录
	 * @param targetUrl
	 * @param sourceUrl
	 * @throws ZipException
	 */
	public static void compressFolder(String targetUrl, File sourceUrl)
			throws ZipException {

		ZipFile zipFile = new ZipFile(targetUrl);

		ZipParameters parameters = new ZipParameters();
		parameters.setCompressionMethod(CompressionMethod.DEFLATE);
		parameters.setCompressionLevel(CompressionLevel.NORMAL);

		zipFile.addFolder(sourceUrl, parameters);
	}
	
	
//	/**
//	 * 压缩一个指定的目录下的文件
//	 *
//	 * @param targetUrl
//	 * @param sourceUrl
//	 * @throws ZipException
//	 * @throws
//	 */
//	public static void compressFolder(OutputStream os, String directoryPath)
//			throws ZipException {
//		File dir = new File(directoryPath);
//		if (!dir.exists()) {
//            return;
//        }
//		compressFolder(directoryPath+File.separator+"压缩.zip", directoryPath);
//		FileInputStream fileInputStream = null;
//		int i;
//		try {
//			fileInputStream = new FileInputStream(directoryPath+File.separator+"压缩.zip");
//			while ((i=fileInputStream.read()) != -1) {
//				os.write(i);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}finally{
//			try {
//				os.close();
//				File file = new File(directoryPath+File.separator+"压缩.zip");
//				file.delete();
//
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//	}
//
//	/**
//	 * 指定一组文件的进行压缩流
//	 *
//	 * @param targetUrl
//	 * @param sourceUrl
//	 * @throws ZipException
//	 * @throws IOException
//	 */
//	public static void compressOutputStream(OutputStream os,
//			List<String> sourceUrls) throws ZipException {
//
//		ZipOutputStream zipFile = new ZipOutputStream(os);
//
//		ZipParameters parameters = new ZipParameters();
//		parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//		parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//
//		File file = null;
//		InputStream inputStream = null;
//		for (String url : sourceUrls) {
//			try {
//				file = new File(url);
//				zipFile.putNextEntry(file, parameters);
//				if (file.isDirectory()) {
//					zipFile.closeEntry();
//					continue;
//				}
//
//				inputStream = new FileInputStream(file);
//				byte[] readBuff = new byte[Integer.parseInt(file.length() + "")];
//				int readLen = -1;
//				while ((readLen = inputStream.read(readBuff)) != -1) {
//					zipFile.write(readBuff, 0, readLen);
//				}
//
//				zipFile.closeEntry();
//			} catch (IOException e) {
//				e.printStackTrace();
//			} finally {
//				try {
//					inputStream.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//
//		}
//
//		try {
//			zipFile.finish();
//			zipFile.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//
//
//	}

}
