|
@@ -0,0 +1,83 @@
|
|
|
+package com.sencorsta.rabs.util;
|
|
|
+
|
|
|
+import java.awt.Desktop;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+public class ZipDirectory {
|
|
|
+ public static void zipDirectory(String sourceDirPath, String zipFilePath) throws IOException {
|
|
|
+ Path p = Paths.get(sourceDirPath);
|
|
|
+ try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(Paths.get(zipFilePath)))) {
|
|
|
+ Files.walk(p)
|
|
|
+ .filter(path -> !Files.isDirectory(path))
|
|
|
+ .forEach(path -> {
|
|
|
+ ZipEntry zipEntry = new ZipEntry(p.relativize(path).toString());
|
|
|
+ try {
|
|
|
+ zs.putNextEntry(zipEntry);
|
|
|
+ Files.copy(path, zs);
|
|
|
+ zs.closeEntry();
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("Error zipping file: " + path + ": " + e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ // 需要压缩的文件和文件夹路径
|
|
|
+ String sourceDirPath1 = "C:\\Users\\daibin\\Desktop\\rabs\\target\\rabs\\rabs.exe";
|
|
|
+ String sourceDirPath2 = "C:\\Users\\daibin\\Desktop\\rabs\\target\\rabs\\opencv\\scene";
|
|
|
+ String zipFilePath = "C:\\Users\\daibin\\Desktop\\rabs\\target\\rabs\\rabs.zip";
|
|
|
+
|
|
|
+ // 创建临时目录来包含要压缩的文件和文件夹
|
|
|
+ Path tempDir = Files.createTempDirectory("tempForZip");
|
|
|
+
|
|
|
+ // 复制文件到临时目录
|
|
|
+ Path tempFile1 = tempDir.resolve("rabs.exe");
|
|
|
+ Files.copy(Paths.get(sourceDirPath1), tempFile1, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+
|
|
|
+ // 复制文件夹到临时目录
|
|
|
+ Path sourcePath2 = Paths.get(sourceDirPath2);
|
|
|
+ Path targetPath2 = tempDir.resolve("opencv\\scene");
|
|
|
+ Files.createDirectories(targetPath2);
|
|
|
+ Files.walk(sourcePath2).forEach(source -> {
|
|
|
+ try {
|
|
|
+ Path target = targetPath2.resolve(sourcePath2.relativize(source).toString());
|
|
|
+ if (Files.isDirectory(source)) {
|
|
|
+ Files.createDirectories(target);
|
|
|
+ } else {
|
|
|
+ Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("Error copying file/folder: " + source + ": " + e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 压缩临时目录
|
|
|
+ zipDirectory(tempDir.toString(), zipFilePath);
|
|
|
+
|
|
|
+ // 删除临时目录及其内容
|
|
|
+ Files.walk(tempDir)
|
|
|
+ .sorted(Comparator.reverseOrder())
|
|
|
+ .map(Path::toFile)
|
|
|
+ .forEach(File::delete);
|
|
|
+
|
|
|
+ System.out.println("Files successfully zipped into: " + zipFilePath);
|
|
|
+
|
|
|
+
|
|
|
+ // 打开目录
|
|
|
+ Desktop desktop = Desktop.getDesktop();
|
|
|
+ desktop.open(new File(zipFilePath).getParentFile());
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("Error creating zip file: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|