Apache Commons Compress 压缩zip

1.序言

官网提供了一个例子,研究了一天才知怎么用。

2.版本

Apache Commons Compress1.12

3.官网例子

public class ScatterSample {
	ParallelScatterZipCreator scatterZipCreator = new ParallelScatterZipCreator();
	ScatterZipOutputStream dirs = ScatterZipOutputStream.fileBased(File.createTempFile("scatter-dirs", "tmp"));

	public ScatterSample() throws IOException {
	}

	public void addEntry(ZipArchiveEntry zipArchiveEntry, InputStreamSupplier streamSupplier) throws IOException {
		if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink())
			dirs.addArchiveEntry(ZipArchiveEntryRequest.createZipArchiveEntryRequest(zipArchiveEntry, streamSupplier));
		else
			scatterZipCreator.addArchiveEntry(zipArchiveEntry, streamSupplier);
	}

	public void writeTo(ZipArchiveOutputStream zipArchiveOutputStream)
			throws IOException, ExecutionException, InterruptedException {
		dirs.writeTo(zipArchiveOutputStream);
		dirs.close();
		scatterZipCreator.writeTo(zipArchiveOutputStream);
	}
}

4.调用方式

public class ZipInputStream implements InputStreamSupplier{

	@Override
	public InputStream get() {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(new File("f:/write.docx"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

}
		ScatterSample scatterSample = new ScatterSample();
		ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(new File("f:/test.zip"));
		ZipArchiveEntry entry = new ZipArchiveEntry("write.docx");
		entry.setMethod(ZipMethod.STORED.getCode());
		scatterSample.addEntry(entry, new ZipInputStream());
		scatterSample.writeTo(zipArchiveOutputStream);
		zipArchiveOutputStream.close();


5.说两句

这个是我的测试,要用的话,请自行整理出来,灵活调用,write.docx是我提前准备压缩进test.zip的文件,您需要事先准备该文件,test.zip是自动生成的,不用管。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值