1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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();
        }
    }
}