File size: 2,434 Bytes
cd6a216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.io.*;

public class Base64Decode {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get(args[0]);
        Path path1 = Paths.get(args[1]);
        // 获取所有分片文件并按名称分组
        File[] files = path.toFile().listFiles();
        if (files == null) {
            return;
        }
        // 按文件名(去除分片后缀)分组
        Map<String, List<String>> fileGroups = new HashMap<>();
        for (File file : files) {
            String fileName = file.getName();
            // 假设分片格式为: 原文件名-0001, 原文件名-0002
            int dashIndex = fileName.lastIndexOf('-');
            if (dashIndex > 0) {
                String partNum = fileName.substring(dashIndex + 1);
                // 检查是否是4位数字的分片编号
                if (partNum.matches("\\d{4}")) {
                    String originalFileName = fileName.substring(0, dashIndex);
                    fileGroups.computeIfAbsent(originalFileName, k -> new ArrayList<>()).add(fileName);
                }
            }
        }
        Base64.Decoder decoder = Base64.getDecoder();
        // 遍历每个文件的分片
        for (java.util.Map.Entry<String, java.util.List<String>> entry : fileGroups.entrySet()) {
            String originalFileName = entry.getKey();
            java.util.List<String> parts = entry.getValue();

            // 按分片编号排序
            parts.sort(String::compareTo);

            // 先解码再合并写入
            Path outputPath = Paths.get(path1.toString(), originalFileName);
            try (FileOutputStream fos = new FileOutputStream(outputPath.toFile())) {
                for (String partName : parts) {
                    Path partPath = Paths.get(path.toString(), partName);
                    try (BufferedReader reader = Files.newBufferedReader(partPath, StandardCharsets.UTF_8)) {
                        String line;
                        while ((line = reader.readLine()) != null && !line.isEmpty()) {
                            byte[] decodedBytes = decoder.decode(line);
                            fos.write(decodedBytes);
                        }
                    }
                }
            }
        }
    }
}