package com.dji.sample.media.util;
|
|
import java.io.*;
|
import java.nio.file.*;
|
import java.util.*;
|
import java.util.zip.*;
|
|
public class MinioFileDownloader {
|
|
private final String bucketPath;
|
|
public MinioFileDownloader(String bucketPath) {
|
this.bucketPath = bucketPath;
|
}
|
|
public void downloadAndZipFolders(List<String> prefixes, String localSaveDir) throws Exception {
|
// 创建目标文件夹路径并生成zip文件名
|
Path localSavePath = Paths.get(localSaveDir);
|
if (!Files.exists(localSavePath)) {
|
Files.createDirectories(localSavePath);
|
}
|
String zipFileName = localSavePath.resolve("compressed_files.zip").toString();
|
|
// 创建压缩文件输出流
|
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName))) {
|
zos.setLevel(Deflater.BEST_COMPRESSION); // 使用最高压缩级别
|
|
// 遍历每个前缀
|
for (String prefix : prefixes) {
|
// 从本地文件系统获取文件
|
String localFolderPath = bucketPath + "/" + prefix;
|
File localFolder = new File(localFolderPath);
|
if (!localFolder.exists() || !localFolder.isDirectory()) {
|
throw new FileNotFoundException("Local folder not found: " + localFolderPath);
|
}
|
|
// 压缩文件夹
|
zipFolder(localFolder, prefix, zos);
|
}
|
}
|
}
|
|
private void zipFolder(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
|
for (File file : folder.listFiles()) {
|
if (file.isDirectory()) {
|
zipFolder(file, parentFolder + "/" + file.getName(), zos);
|
continue;
|
}
|
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
|
try (FileInputStream fis = new FileInputStream(file)) {
|
byte[] buffer = new byte[1024];
|
int length;
|
while ((length = fis.read(buffer)) >= 0) {
|
zos.write(buffer, 0, length);
|
}
|
}
|
zos.closeEntry();
|
}
|
}
|
|
public static void main(String[] args) {
|
try {
|
String bucketPath = "/data/software/minio-data/cloud-bucket";
|
List<String> prefixes = List.of(
|
"c88e9f12-9fab-4aca-8fc4-b02d0b0ce5f2/DJI_202407151726_001_c88e9f12-9fab-4aca-8fc4-b02d0b0ce5f2/"
|
);
|
|
MinioFileDownloader downloader = new MinioFileDownloader(bucketPath);
|
|
// 下载并压缩文件到本地目录
|
String localSaveDir = "/path/to/local/save/directory"; // 修改为你想保存的本地目录
|
downloader.downloadAndZipFolders(prefixes, localSaveDir);
|
|
System.out.println("压缩文件已保存到:" + localSaveDir);
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|