办学质量监测教学评价系统
ageerle
2025-05-23 cd7e07bea6e221933b76e6d4dc86880dbd776af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.ruoyi.utils;
 
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * ZIP文件处理工具类
 */
public class ZipUtils {
 
    /**
     * 解压ZIP文件到指定目录
     *
     * @param zipData ZIP文件的字节数组
     * @param destDir 目标目录
     * @return 解压后的文件路径列表
     * @throws IOException 如果解压过程中发生错误
     */
    public static String[] unzip(byte[] zipData, String destDir) throws IOException {
        File destDirFile = new File(destDir);
        if (!destDirFile.exists()) {
            destDirFile.mkdirs();
        }
 
        List<String> extractedPaths = new ArrayList<>();
        try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData);
             ZipInputStream zis = new ZipInputStream(bis)) {
 
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                String filePath = destDir + File.separator + zipEntry.getName();
                if (!zipEntry.isDirectory()) {
                    extractFile(zis, filePath);
                    extractedPaths.add(filePath);
                } else {
                    new File(filePath).mkdirs();
                }
                zis.closeEntry();
            }
        }
        return extractedPaths.toArray(new String[0]);
    }
 
    private static void extractFile(ZipInputStream zis, String filePath) throws IOException {
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
            byte[] buffer = new byte[4096];
            int read;
            while ((read = zis.read(buffer)) != -1) {
                bos.write(buffer, 0, read);
            }
        }
    }
 
    /**
     * 解压ZIP文件并返回文件内容的Base64编码字符串数组
     *
     * @param zipData ZIP文件的字节数组
     * @return Base64编码的文件内容数组
     * @throws IOException 如果解压过程中发生错误
     */
    public static String[] unzipForBase64(byte[] zipData) throws IOException {
        List<String> base64Contents = new ArrayList<>();
        try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData);
             ZipInputStream zis = new ZipInputStream(bis)) {
 
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                if (!zipEntry.isDirectory()) {
                    // 读取文件内容到内存
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[4096];
                    int read;
                    while ((read = zis.read(buffer)) != -1) {
                        baos.write(buffer, 0, read);
                    }
                    
                    // 将文件内容转换为Base64字符串
                    String base64Content = Base64.getEncoder().encodeToString(baos.toByteArray());
                    base64Contents.add(base64Content);
                }
                zis.closeEntry();
            }
        }
        return base64Contents.toArray(new String[0]);
    }
 
  /**
   * 解压ZIP文件并返回MultipartFile数组
   *
   * @param zipData ZIP文件的字节数组
   * @return MultipartFile数组
   * @throws IOException 如果解压过程中发生错误
   */
  public static MultipartFile[] unzipToMultipartFiles(byte[] zipData) throws IOException {
    List<MultipartFile> multipartFiles = new ArrayList<>();
    try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData);
        ZipInputStream zis = new ZipInputStream(bis)) {
 
      ZipEntry zipEntry;
      while ((zipEntry = zis.getNextEntry()) != null) {
        if (!zipEntry.isDirectory()) {
          // 读取文件内容到内存
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          byte[] buffer = new byte[4096];
          int read;
          while ((read = zis.read(buffer)) != -1) {
            baos.write(buffer, 0, read);
          }
 
          // 创建MultipartFile对象
          String fileName = zipEntry.getName();
          byte[] content = baos.toByteArray();
          String contentType = determineContentType(fileName);
 
          MultipartFile multipartFile = new MockMultipartFile(
              fileName,                  // 文件名
              fileName,                  // 原始文件名
              contentType,               // 内容类型
              content                    // 文件内容
          );
 
          multipartFiles.add(multipartFile);
        }
        zis.closeEntry();
      }
    }
    return multipartFiles.toArray(new MultipartFile[0]);
  }
 
  /**
   * 根据文件名确定内容类型
   *
   * @param fileName 文件名
   * @return 内容类型
   */
  private static String determineContentType(String fileName) {
    String extension = "";
    int i = fileName.lastIndexOf('.');
    if (i > 0) {
      extension = fileName.substring(i + 1).toLowerCase();
    }
 
    switch (extension) {
      case "txt":
        return "text/plain";
      case "html":
      case "htm":
        return "text/html";
      case "pdf":
        return "application/pdf";
      case "jpg":
      case "jpeg":
        return "image/jpeg";
      case "png":
        return "image/png";
      case "gif":
        return "image/gif";
      case "doc":
      case "docx":
        return "application/msword";
      case "xls":
      case "xlsx":
        return "application/vnd.ms-excel";
      case "xml":
        return "application/xml";
      case "json":
        return "application/json";
      default:
        return "application/octet-stream";
    }
  }
}