test-any-format / Base64Decode.java
yangyingjie1997's picture
Create Base64Decode.java
cd6a216 unverified
Raw History Blame Contribute Delete
2.43 kB
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);
}
}
}
}
}
}
}