|
@@ -24,13 +24,20 @@ public class ZipUtils {
|
|
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(zipFilePath);
|
|
|
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
|
|
+ zipDirectory(sourceFolder, "", zos);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- File[] files = sourceFolder.listFiles();
|
|
|
- if (files != null) {
|
|
|
- for (File file : files) {
|
|
|
- if (file.isFile()) {
|
|
|
- zipFile(file, file.getName(), zos);
|
|
|
- }
|
|
|
+ private static void zipDirectory(File folder, String baseName, ZipOutputStream zos) throws IOException {
|
|
|
+ File[] files = folder.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ // 递归处理子目录,更新基础名称
|
|
|
+ zipDirectory(file, baseName + file.getName() + File.separator, zos);
|
|
|
+ } else {
|
|
|
+ // 压缩文件,使用基础名称作为路径
|
|
|
+ zipFile(file, baseName + file.getName(), zos);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -102,4 +109,13 @@ public class ZipUtils {
|
|
|
}
|
|
|
return flag.get();
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ zipFolderFiles("C:\\home\\ObjectDetection_Web\\upload\\2024\\10\\12\\VEDAI_20241012085201A001_to_infrared",
|
|
|
+ "C:\\home\\ObjectDetection_Web\\upload\\2024\\10\\12\\VEDAI_20241012085201A001_to_infrared.zip");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|