java 解压MultipartFile 文件
时间: 2023-11-09 17:02:08 浏览: 203
可以使用Java的ZipInputStream类来解压MultipartFile文件。具体步骤如下:
1. 获取MultipartFile文件的输入流
2. 创建ZipInputStream对象,将MultipartFile文件的输入流传入
3. 循环读取ZipInputStream中的ZipEntry对象,将每个ZipEntry对象的内容写入到输出流中
4. 关闭输入流和输出流
以下是示例代码:
```
public void unzip(MultipartFile file) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream());
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int len;
while ((len = zipInputStream.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
}
```
阅读全文