qitz vor 2 Jahren
Ursprung
Commit
3eeb1a0882

+ 54
- 0
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/ZipFileUtils.java Datei anzeigen

@@ -0,0 +1,54 @@
1
+package com.ruoyi.wisdomarbitrate.utils;
2
+
3
+import java.io.File;
4
+import java.io.FileInputStream;
5
+import java.io.FileOutputStream;
6
+import java.io.IOException;
7
+import java.util.zip.ZipEntry;
8
+import java.util.zip.ZipOutputStream;
9
+
10
+public class ZipFileUtils {
11
+
12
+    public static void main(String[] args) {
13
+        String filePath1 = "/path/to/file1.txt";
14
+        String filePath2 = "/path/to/file2.txt";
15
+
16
+        // ZIP文件的输出路径
17
+        String zipOutPath = "/path/to/output.zip";
18
+
19
+        try {
20
+            FileOutputStream fous = new FileOutputStream(zipOutPath);
21
+            ZipOutputStream zipOutstream = new ZipOutputStream(fous);
22
+            FileInputStream fis1 = new FileInputStream(filePath1);
23
+            FileInputStream fis2 = new FileInputStream(filePath2);
24
+
25
+            zipFile(filePath1, fis1, zipOutstream);
26
+
27
+            zipFile(filePath2, fis2, zipOutstream);
28
+
29
+            System.out.println("文件成功打包成ZIP文件!");
30
+        } catch (IOException e) {
31
+            e.printStackTrace();
32
+        }
33
+    }
34
+
35
+
36
+
37
+
38
+
39
+
40
+    private static void zipFile(String zipfilePath, FileInputStream zipFileinsteam, ZipOutputStream zipfileOut)
41
+            throws IOException {
42
+        ZipEntry zipfileEntry = new ZipEntry(new File(zipfilePath).getName());
43
+        zipfileOut.putNextEntry(zipfileEntry);
44
+
45
+        // 从输入流读取数据并写入ZIP文件
46
+        byte[] bytes = new byte[1024];
47
+        int length;
48
+        while ((length = zipFileinsteam.read(bytes)) >= 0) {
49
+            zipfileOut.write(bytes, 0, length);
50
+        }
51
+        zipfileOut.closeEntry();
52
+        zipFileinsteam.close();
53
+    }
54
+}