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();
}
}
}
minio压缩下载文件夹内所有文件
最新推荐文章于 2023-08-31 13:32:39 发布