java 文件夹压缩

使用 java 模拟 压缩工具,压缩文件夹。例如把D:\abc 目录下的所有文件压缩。

/****
	 * filedir to zip dir
	 * 
	 * @param inputFileName 要压缩的文件
	 * @param zipFileName 创建的压缩文件
	 * @throws Exception
	 */
	public void zip(String inputFileName, String zipFileName) throws Exception {
		zipFile(zipFileName, new File(inputFileName));
	}
/****
	 * 
	 * @param zipFileName 
	 * @param inputFile
	 * @throws Exception
	 */
	private void zipFile(String zipFileName, File inputFile) throws Exception {
		ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(new File(zipFileName));
		zipOutput.setEncoding("GBK");//防止压缩文件名为中文乱码问题
		zipProcess(zipOutput, inputFile, null);
		zipOutput.closeArchiveEntry();
		zipOutput.close();
	}
 
/***
	 * zip to file process
	 * 
	 * @param zipOutput
	 * @param inputFile
	 * @param entry
	 * @throws IOException
	 */
	private void zipProcess(ZipArchiveOutputStream zipOutput, File inputFile, ZipArchiveEntry entry) throws IOException {
		if (null != inputFile && null != zipOutput) {
			if (inputFile.isDirectory()) {
				File[] fls = inputFile.listFiles();
				for (int i = 0; i < fls.length; i++) {
					zipProcess(zipOutput, fls[i], new ZipArchiveEntry(fls[i].getName()));
				}
			} else {// this file is file
				if (null != entry) {
					zipOutput.putArchiveEntry(entry);
					FileInputStream in = new FileInputStream(inputFile);
					int b;
					while ((b = in.read()) != -1) {
						zipOutput.write(b);
					}
					in.close();
				}
			}
		}
	} 
public static void main(String[] args) throws Exception {
		DownloadUploadifyUtil downloadUploadifyUtil = new DownloadUploadifyUtil();
		downloadUploadifyUtil.zip("D:\\abc", "d:\\测试_cc.zip");
	}

  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxpp688

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值