`
yuanhuiwu
  • 浏览: 87924 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java调用ant进行压缩和解压

    博客分类:
  • ant
阅读更多

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
 * zip工具
 * 
 * @author yuanhuiwu
 * 
 */
public class ZipUtils {
	private static Logger log = Logger.getLogger(ZipUtils.class);
	private static final int BUFFER_SIZE = 8 * 1024;

	/**
	 * @param source
	 *            zip源文件
	 * @param dest
	 *            解压保存的目录
	 * @throws IOException
	 *             IO异常
	 */
	public static void unzip(File source, File dest) throws IOException {
		ZipFile zipFile = null;
		try {
			zipFile = new ZipFile(source);
			unzip(zipFile, dest);
		} finally {
			if (zipFile != null) {
				zipFile.close(); // 关闭文件
			}
		}
	}

	/**
	 * 解压zip文件
	 * 
	 * @param source
	 *            zip 源文件
	 * @param dest
	 *            输出目录
	 * @throws IOException
	 *             IO异常
	 */
	@SuppressWarnings("unchecked")
	private static void unzip(ZipFile source, File dest) throws IOException {

		// 创建目录
		if (!dest.exists()) { // 不存在
			dest.mkdirs(); // 创建
		}
		Enumeration<ZipEntry> en = source.getEntries(); // 返回所有条目 Returns all

		while (en.hasMoreElements()) {
			ZipEntry zipEntry = en.nextElement();

			if (zipEntry.isDirectory()) {// 目录,则创建
				File dir = new File(dest, zipEntry.getName());
				if (!dir.exists()) {
					dir.mkdirs();
					dir = null;
				}
				continue;
			}
			// 创建文件
			File file = new File(dest, zipEntry.getName());
			if (file.exists()) {
				file.delete();
			} else if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			// end 创建文件
			// 写入
			InputStream in = null;
			FileOutputStream out = null;
			try {
				in = source.getInputStream(zipEntry);
				out = new FileOutputStream(file);
				copy(in, out);
			} finally {// 每次写入后都要关闭流,防止占用资源,导致不能其它操作(如删除该文件)!!!!!!
				file = null;
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			}
		}
	}

	/**
	 * @param in
	 *            输入流
	 * @param out
	 *            输出流
	 * @throws IOException
	 *             IO异常
	 */
	private static void copy(InputStream in, OutputStream out)
			throws IOException {
		try {
			byte[] buffer = new byte[BUFFER_SIZE];
			int count = 0;
			do {
				if (count != 0) {
					out.write(buffer, 0, count);
				}
				count = in.read(buffer, 0, buffer.length);
			} while (count != -1);
		} finally {
			if (out != null) {
				out.flush();
			}
		}
	}

	/**
	 * 压缩成zip格式
	 * 
	 * @param source
	 *            源文件或文件夹
	 * @param dest
	 *            输出的zip文件
	 * @throws IOException
	 *             IO异常
	 */
	public static void zip(File source,File dest) throws IOException {
		ZipOutputStream out = null;
		FileOutputStream fos = null;
		try {
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs();
			}
			if(dest.exists()){
				dest.delete();
			}
			dest.createNewFile();
			
			fos = new FileOutputStream(dest);
			out = new ZipOutputStream(fos);
			// out.setMethod(ZipOutputStream.DEFLATED);
			// out.setLevel(Deflater.BEST_COMPRESSION);
			zip(out, source, null);
		} finally {
			if (out != null) {
				out.close();
			}
			if (fos != null) {
				fos.close();
			}
		}
	}

	/**
	 * 压缩成zip格式
	 * 
	 * @param out
	 *            ZipOutputStream
	 * @param source
	 *            File
	 * @param vpath
	 *            String
	 * @throws IOException
	 *             IO异常
	 */
	private static void zip(ZipOutputStream out, File source, String vpath)
			throws IOException {
		if (source.isDirectory()) { // 目录
			if(vpath == null){
				vpath = source.getName()+"/";
			}else{
				vpath = vpath +"/";
			}
			out.putNextEntry(new ZipEntry(vpath)); // 格式如:dir/
			File[] files = source.listFiles();
			for (int i = 0; i < files.length; i++) {
				zip(out, files[i], vpath + files[i].getName()); // 递归
			}
		} else {// 文件
			if(vpath == null){
				vpath = source.getName();
			}
			ZipEntry ze = new ZipEntry(vpath);
			// ze.setMethod(ZipEntry.DEFLATED);
			out.putNextEntry(ze);

			FileInputStream in = null;
			try {
				in = new FileInputStream(source);
				copy(in, out);
			} finally {
				if (in != null) {
					in.close();
				}
			}
		}
	}
//
//	public static void unzip2(File source, File dest) {
//		Project prj = new Project();
//		Expand expand = new Expand();
//		expand.setProject(prj);
//		expand.setSrc(source);
//		expand.setDest(dest);
//		expand.execute();
//	}
//
//	public static void zip2(File dest, File source) {
//		Project prj = new Project();
//		Zip zip = new Zip();
//		zip.setProject(prj);
//		zip.setDestFile(dest);
//		FileSet fileSet = new FileSet();
//		fileSet.setProject(prj);
//		fileSet.setDir(source);
//		fileSet.setDefaultexcludes(false);
//		zip.addFileset(fileSet);
//		zip.execute();
//	}
}

分享到:
评论

相关推荐

    [原]使用Ant实现zip压缩解压功能

    以下是一个简单的示例,展示了如何使用Ant压缩一组文件到一个ZIP文件: ```xml **/*"/&gt; ``` 在这个例子中,`&lt;zip&gt;`任务创建了一个名为“output.zip”的ZIP文件,`&lt;fileset&gt;`定义了要被压缩的文件或...

    java 中 zip压缩文件解压工具类

    虽然Ant不直接用于解压缩文件,但可以与Java代码结合使用,例如通过Ant任务来调用`CompressFileUtils`进行解压缩操作。Ant提供了一系列的任务,如`unzip`,可以直接在构建脚本中用于解压缩文件。 至于标签“文件解...

    java zip 压缩 解压缩 附带ant.jar

    在本主题中,我们将深入探讨如何使用Java来对文件进行zip压缩和解压缩,并提及`ant.jar`的作用。 首先,让我们来看一下`JavaZip.java`这个文件。它很可能包含了一个示例程序,演示了如何使用Java API来压缩和解压缩...

    java 操作Zip文件(压缩、解压、加密).zip

    Java操作Zip文件主要涉及到对文件和目录的压缩与解压缩,以及在必要时对压缩文件进行加密处理。这里我们重点讨论使用两个库:`zip4j`和`Apache Ant`来实现这些功能。 1. **zip4j库**:`zip4j-1.3.2.jar`是一个用...

    java操作压缩文件和解压文件实例代码(经测试)

    虽然Ant本身不是直接用于文件压缩,但它包含的`ZipTask`和`UnzipTask`可以方便地在Ant脚本中进行文件压缩和解压。如果`ZipFileList.java`使用了Ant的功能,那么可能通过`org.apache.tools.ant.types.FileSet`选择要...

    利用ant.jar解压缩zip包

    此外,如果你需要在命令行环境中使用ant.jar,可以编写一个简单的Ant构建文件(build.xml),并使用`unzip`任务来解压缩ZIP文件。例如: ```xml ``` 然后通过命令行运行`ant unzip`即可。 总的来说,利用...

    ant操作7zip压缩、解压 tar.gz

    了解这些基础知识后,我们可以更有效地利用7-Zip和Ant处理文件压缩和解压,特别是在大型项目中,自动化这些过程可以显著提高工作效率。同时,理解XML配置文件如build.xml的内容,可以帮助我们定制化构建流程,满足...

    java Zip压缩解压

    总结来说,"java Zip压缩解压"涉及了Java标准库中的`java.util.zip`包,用于创建和读取ZIP文件;Apache Ant作为构建工具,可能用于自动化压缩和解压过程;而JUnit则用于编写和运行测试,确保代码的正确实现。在实际...

    JAVA解压ZIP多层目录文件(需ant.jar

    ### JAVA解压ZIP多层目录文件(需ant.jar) #### 概述 本文将详细介绍一个Java方法,该方法用于解压包含多层目录结构的ZIP文件,并能够支持中文文件名。这种方法利用了Apache Ant库中的`org.apache.tools.zip....

    java的Ant打包工具

    Ant的核心概念是基于任务(task)的,每个任务都是一个特定的Java类实现,可以执行诸如编译、拷贝、压缩、解压、单元测试等操作。在XML配置文件中,这些任务被组织成一系列目标(target),形成一个有序的构建流程。例如...

    ant编译java工程

    解压后,用户可以按照`build.xml`中的指示使用Ant进行编译和构建。 总结来说,Ant编译Java工程涉及以下知识点: 1. Apache Ant的基本概念和作用。 2. `build.xml`的结构和配置,包括目标和任务的定义。 3. 编译Java...

    解压zip压缩文件,支持多文件目录解压,中文乱码问题

    在使用Java对ZIP压缩文件进行解压的方式中有两种,一种是使用apache提供的ant.jar工具包,但是如果ZIP文件中含有中文名称的文件,在解压后,文件名将出现乱码,另一种是使用Java自身JDK中java.util.zip包下工具类,...

    用ant来解压文件

    在本文中,我们将探讨如何使用Ant来解压文件,这对于自动化构建流程尤其有用。 Ant提供了`unzip`和`untar`任务,分别用于解压ZIP和TAR格式的文件。在描述中提到的博文链接虽然不可访问,但通常会详细解释如何配置...

    java课程设计之批量压缩文件

    通过这个项目,学生可以学习到如何使用Java API来处理文件,如何使用第三方库如Apache Ant进行文件压缩,以及如何设计和实现一个批处理程序。这些都是Java开发者必备的技能,对于提升实际开发能力非常有帮助。同时,...

    java压缩zip文件解决中文乱码问题

    在Java编程中,处理文件压缩和解压是常见的任务,特别是使用ZIP格式。然而,当涉及到包含中文字符的文件或目录时,可能会遇到乱码问题。这个问题主要源于字符编码的不一致,通常需要正确设置字符集来确保中文字符在...

    java解压和压缩ZIP文件的功能

    这篇内容将深入讲解如何使用Java实现ZIP文件的压缩和解压功能,并结合给定的`ant.jar`库和`ZipUtil.java`源代码文件来理解其实现原理。 首先,`ant.jar`是Apache Ant的库文件,Ant是一个基于Java的构建工具,它可以...

    java解压jar包及示例

    在Java编程环境中,解压和压缩文件是常见的需求,特别是在处理资源文件、打包应用程序或进行自动化构建时。本文将深入探讨如何使用Java实现RAR压缩和解压,以及如何利用提供的jar包(java-unrar-0.3.jar和ant.jar)...

Global site tag (gtag.js) - Google Analytics