minio压缩下载文件夹内所有文件

MinioClient client= new MinioClient(serverUrl,accesskey,secretkey);
String path = "bucketname/demo/0001/";
// /demo/0001
String filePath = path.substring(path.indexOf("/")+1, path.lastIndexOf("/"));
// 获取文件夹内所有内容
Iterable<Result<Item>> myObjects = client.listObjects(MINIO_BUCKET_MODEL, filePath);
List<String> filePaths = new ArrayList<>();
for (Result<Item> result : myObjects) {
    Item item = result.get();
    filePaths.add(item.objectName());
}

// 以ZIP文件格式写入文件
ZipOutputStream zipos = null;
DataOutputStream os = null;
InputStream is = null;
try {
	response.reset();
	String zipName = new String(URLEncoder.encode(fileName, "UTF-8").getBytes(), StandardCharsets.ISO_8859_1);
	response.setHeader("Content-Disposition", "attachment;fileName=\"" + zipName + ".zip\"");
	response.setContentType("application/octet-stream; charset=UTF-8");
	zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
	zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
	for(int i = 0;i < filePaths.size();i++) {
		String file = filePaths.get(i);
		String packageName = filePaths.get(i).replace(filePath+"/", "");
		try {
			zipos.putNextEntry(new ZipEntry(packageName));
			os = new DataOutputStream(zipos);
			is = client.getObject(MINIO_BUCKET_MODEL, file);
			byte[] b = new byte[1024];
			int length = 0;
			while((length = is.read(b))!= -1){
				os.write(b, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	is.close();
	os.flush();
	os.close();
	zipos.close();
} catch (Exception e) {
	e.printStackTrace();
}finally {
	if (is != null) {
		try {
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	if (os != null) {
		try {
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	if (zipos != null) {
		try {
			zipos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值