/*
 * Copyright 2015 Petasoft Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.petasoft.export.util.zip;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;

/**
 * @author ���� 2015-05-15
 */
public class Zip {

	/**
	 * ʹ��zipѹ��ļ�
	 * 
	 * @param source
	 *            ��Ҫѹ����ļ�
	 * @param target
	 *            ѹ����ļ��Ĵ��·��
	 * @param delflag
	 *            ѹ����Ƿ�ɾ��Դ�ļ�
	 */
	public static void compress(File source, File target, boolean delflag) {
		if (!target.exists()) {
			target.mkdirs();
		}
		File zipFile = new File(target.getAbsolutePath() + File.separator
				+ source.getName() + ".zip");
		Project prj = new Project();
		org.apache.tools.ant.taskdefs.Zip zip = new org.apache.tools.ant.taskdefs.Zip();
		zip.setProject(prj);
		zip.setDestFile(zipFile);
		FileSet fileSet = new FileSet();
		fileSet.setProject(prj);
		fileSet.setDir(target);
		// ������Щ�ļ����ļ���
		fileSet.setIncludes(source.getName());
		// �ų���Щ�ļ����ļ���
		fileSet.setExcludes("*.zip");
		zip.addFileset(fileSet);
		zip.execute();
		if (delflag) {
			source.delete();
		}
	}
}