【Java】利用ant插件压缩文件夹及其所有子文件与子文件夹

如果在Java要压缩一个文件夹及其所有子文件与子文件夹,可以利用到Apache公司提供的ant插件。其实也就是一个jar包。

比如,如果要把f:\bb下的所有文件,压缩成一个f:\bb.zip,如下图:


首先先到Apache的官网,下载ant插件,地址:http://ant.apache.org/bindownload.cgi(点击打开链接


下载解压之后,取走其中的apache-ant-1.9.4\lib下的ant.jar放到你的java工程就行:


比如拷贝到你的java工程,目录结构如下图,在Eclipse右键你的java工程properties->java build path->add jars选择你刚才拷贝过来的ant.jar:


之后,首先在开头引入如下的包:

package zipTest;

import java.io.*;
import java.util.*;

import org.apache.tools.zip.*;

接着如《【Java】读取其下所有文件夹与文件的路径》( 点击打开链接)一样,先读取你要压缩的文件夹的所有文件路径,用一个ArrayList<String>存起来,然后如《【Java】ArrayList<String>转化为String数组问题》( 点击打开链接)将其转化成String数组

public static void main(String[] args) throws Exception {
	//先读取f:/bb下的所有文件的路径
	ArrayList<String> dirStrArr = new ArrayList<String>();
	File dir = new File("f:/bb");
	if (dir.exists()) {
		File files[] = dir.listFiles();
		for (File file : files) {
			dirStrArr.add(dir.getPath() + "/" + file.getName());
		}
	}
	//输出为f:/bb.zip,接着要求要求刚才读取到所有文件
	compress(new FileOutputStream("f:/bb.zip"),
			(String[]) dirStrArr.toArray(new String[0]));
}
再通过如下的Java方法,将其压缩:

//如果是文件夹,则循环递归这个方法,因为还要读取旗下的所有子文件夹与子文件。
private static void zipDirectory(ZipOutputStream zos, String dirName,
		String basePath) throws Exception {
	File dir = new File(dirName);
	if (dir.exists()) {
		File files[] = dir.listFiles();
		
		if (files.length > 0) {
			for (File file : files) {
				//读取的时候同样分两种情况,文件、文件夹
				if (file.isDirectory()) {
					zipDirectory(
							zos,
							file.getPath(),
							basePath
									+ file.getName().substring(
											file.getName().lastIndexOf(
													File.separator) + 1)
									+ File.separator);
				} else
					zipFile(zos, file.getPath(), basePath);
			}
			//如果是一个空文件夹
		} else {
			//直接压缩就可以了
			ZipEntry ze = new ZipEntry(basePath);
			zos.putNextEntry(ze);
		}
	}
}

private static void zipFile(ZipOutputStream zos, String filename,
		String basePath) throws Exception {
	//如果是文件,应该利用输入输出流,分段压缩
	File file = new File(filename);
	if (file.exists()) {
		FileInputStream fis = new FileInputStream(filename);
		ZipEntry ze = new ZipEntry(basePath + file.getName());
		zos.putNextEntry(ze);
		byte[] buffer = new byte[8192];
		int count = 0;
		while ((count = fis.read(buffer)) > 0) {
			zos.write(buffer, 0, count);
		}
		fis.close();
	}
}

public static void compress(OutputStream os, String... paths)
		throws Exception {
	// 指定输出的地方就是传过来的os
	ZipOutputStream zos = new ZipOutputStream(os);
	// 对传递过来的路径进行遍历
	for (String path : paths) {
		// 如果传递过来的路径为空,则不进行这次循环,以免出错
		if (path.equals(""))
			continue;
		File file = new File(path);
		// 之后,如果这个路径存在,则分两种情况,一种是文件夹,另一种是文件
		if (file.exists()) {
			if (file.isDirectory()) {
				zipDirectory(zos, file.getPath(), file.getName()
						+ File.separator);
			} else {
				zipFile(zos, file.getPath(), "");
			}
		}
	}
	zos.close();
}

整个文件连起来是这样的:

package zipTest;

import java.io.*;
import java.util.*;

import org.apache.tools.zip.*;

public class zipTest {

	// 如果是文件夹,则循环递归这个方法,因为还要读取旗下的所有子文件夹与子文件。
	private static void zipDirectory(ZipOutputStream zos, String dirName,
			String basePath) throws Exception {
		File dir = new File(dirName);
		if (dir.exists()) {
			File files[] = dir.listFiles();

			if (files.length > 0) {
				for (File file : files) {
					// 读取的时候同样分两种情况,文件、文件夹
					if (file.isDirectory()) {
						zipDirectory(
								zos,
								file.getPath(),
								basePath
										+ file.getName().substring(
												file.getName().lastIndexOf(
														File.separator) + 1)
										+ File.separator);
					} else
						zipFile(zos, file.getPath(), basePath);
				}
				// 如果是一个空文件夹
			} else {
				// 直接压缩就可以了
				ZipEntry ze = new ZipEntry(basePath);
				zos.putNextEntry(ze);
			}
		}
	}

	private static void zipFile(ZipOutputStream zos, String filename,
			String basePath) throws Exception {
		// 如果是文件,应该利用输入输出流,分段压缩
		File file = new File(filename);
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(filename);
			ZipEntry ze = new ZipEntry(basePath + file.getName());
			zos.putNextEntry(ze);
			byte[] buffer = new byte[8192];
			int count = 0;
			while ((count = fis.read(buffer)) > 0) {
				zos.write(buffer, 0, count);
			}
			fis.close();
		}
	}

	public static void compress(OutputStream os, String... paths)
			throws Exception {
		// 指定输出的地方就是传过来的os
		ZipOutputStream zos = new ZipOutputStream(os);
		// 对传递过来的路径进行遍历
		for (String path : paths) {
			// 如果传递过来的路径为空,则不进行这次循环,以免出错
			if (path.equals(""))
				continue;
			File file = new File(path);
			// 之后,如果这个路径存在,则分两种情况,一种是文件夹,另一种是文件
			if (file.exists()) {
				if (file.isDirectory()) {
					zipDirectory(zos, file.getPath(), file.getName()
							+ File.separator);
				} else {
					zipFile(zos, file.getPath(), "");
				}
			}
		}
		zos.close();
	}

	public static void main(String[] args) throws Exception {
		// 先读取f:/bb下的所有文件的路径
		ArrayList<String> dirStrArr = new ArrayList<String>();
		File dir = new File("f:/bb");
		if (dir.exists()) {
			File files[] = dir.listFiles();
			for (File file : files) {
				dirStrArr.add(dir.getPath() + "/" + file.getName());
			}
		}
		// 输出为f:/bb.zip,接着要求要求刚才读取到所有文件
		compress(new FileOutputStream("f:/bb.zip"),
				(String[]) dirStrArr.toArray(new String[0]));
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值