package org.sxkj.common.utils.file; import org.sxkj.common.constant.CommonConstant; import org.sxkj.common.model.OdmUrlDto; import java.io.*; import java.nio.file.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.zip.*; public class FileDownloader { public FileDownloader() { // 构造方法 } // 按照 jobId 分类下载并压缩文件 public void downloadAndZipFilesByJobId(Map> jobIdToFilePathsMap, String localSaveDir, String zipFileName) throws Exception { // 创建目标文件夹路径 Path localSavePath = Paths.get(localSaveDir); if (!Files.exists(localSavePath)) { Files.createDirectories(localSavePath); } String zipFilePath = localSavePath.resolve(zipFileName).toString(); // 创建压缩文件输出流 try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) { zos.setLevel(Deflater.BEST_COMPRESSION); // 使用最高压缩级别 // 遍历每个 waylineJobId 对应的文件列表 for (Map.Entry> entry : jobIdToFilePathsMap.entrySet()) { // waylineJobId 文件夹名 String key = entry.getKey(); // 文件路径列表 List odmUrlDtos = entry.getValue(); // 将每个文件添加到 waylineJobId 文件夹中 for (OdmUrlDto odmUrlDto : odmUrlDtos) { // String fullFilePath = "E:\\work\\temp\\odm\\odm_orthophoto.tif"; String fullFilePath = odmUrlDto.getPath(); File file = new File(fullFilePath); if (!file.exists()) { throw new FileNotFoundException("File not found: " + fullFilePath); } // 压缩文件到对应的 waylineJobId 文件夹 zipFile(file, key,odmUrlDto.getDkbh(), zos); } } } } /** * 将文件压缩到指定的 jobId 文件夹下 * @param file * @param key 文件夹 * @param dkbh * @param zos * @throws IOException */ private void zipFile(File file, String key,String dkbh, ZipOutputStream zos) throws IOException { // 将文件放入以 jobIdFolder 命名的文件夹中 String entryName = key + "/" + dkbh + ".tif"; zos.putNextEntry(new ZipEntry(entryName)); 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 long convertToTimestampString(String dateStr) { // 定义日期格式 SimpleDateFormat dateFormat = new SimpleDateFormat(CommonConstant.YYYY_MM_DD_HH_MM_SS); try { // 将字符串解析为Date对象 Date date = dateFormat.parse(dateStr); // 获取时间戳(毫秒) return date.getTime(); } catch (java.text.ParseException e) { throw new RuntimeException(e); } } }