Java實現(xiàn)解析zip壓縮包并獲取文件內(nèi)容
功能描述
頁面上傳一個源碼壓縮包,后端將壓縮包解壓,并獲取每個文件中的內(nèi)容。
相關(guān)源碼
(1)首先定義一個與解壓文件對應(yīng)的實體類。
package com.sonar.data.vo;
import lombok.Data;
/**
* 文件解析對象
*
* @author Yuanqiang.Zhang
* @since 2022/7/12
*/
@Data
public class UnzipFileVo {
/**
* 類型:0-文件夾;1-文件
*/
private Integer type;
/**
* 文件路徑(如:src/main/java/com/zyq/entity/User.java)
*/
private String path;
/**
* 文件內(nèi)容
*/
private String content;
}
(2)接下來就是解壓的工具類了。
package com.sonar.data.utils.business;
import com.sonar.data.vo.UnzipFileVo;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* 解析文件工具類
*
* @author Yuanqiang.Zhang
* @since 2022/7/12
*/
@SuppressWarnings("unused")
public class UnZipUtils {
public static int BYTE_LEN = 10240;
/**
* 本地文件解壓
*
* @param file 具體文件
* @return 解壓后的文件列表
*/
public static List<UnzipFileVo> unzip(File file) {
if (Objects.isNull(file) || !file.exists()) {
return Collections.emptyList();
}
ZipFile zip = null;
try {
zip = new ZipFile(file);
} catch (IOException e) {
e.printStackTrace();
}
if (Objects.isNull(zip)) {
return Collections.emptyList();
}
Enumeration<? extends ZipEntry> entries = zip.entries();
List<UnzipFileVo> vos = new ArrayList<>();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String path = entry.getName();
UnzipFileVo vo = new UnzipFileVo();
vo.setPath(path);
// 解析文件夾
boolean directory = entry.isDirectory();
if (directory) {
vo.setType(0);
vos.add(vo);
continue;
}
// 解析文件
vo.setType(1);
StringBuilder sb = new StringBuilder();
try (InputStream in = zip.getInputStream(entry);
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
vo.setContent(sb.toString());
vos.add(vo);
}
if (Objects.nonNull(zip)) {
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return vos;
}
/**
* 上傳文件解壓
*
* @param multipartFile 上傳文件
* @return 解壓后的文件列表
*/
public static List<UnzipFileVo> unzip(MultipartFile multipartFile) {
File file = getFile(multipartFile);
if (Objects.isNull(file)) {
return Collections.emptyList();
}
List<UnzipFileVo> vos = unzip(file);
if (file.exists()) {
boolean delete = file.delete();
if (delete) {
System.out.println(file.getName() + " 臨時文件刪除成功!");
} else {
System.out.println(file.getName() + " 臨時文件刪除失敗!");
}
}
return vos;
}
/**
* MultipartFile 轉(zhuǎn) File
*
* @param multipartFile 上傳文件
* @return 本地文件
*/
private static File getFile(MultipartFile multipartFile) {
String fileName = System.currentTimeMillis() + "_" + multipartFile.getOriginalFilename();
File file = new File("D://" + fileName);
int len;
try (OutputStream os = new FileOutputStream(file);
InputStream in = multipartFile.getInputStream()) {
byte[] buffer = new byte[BYTE_LEN];
while ((len = in.read(buffer, 0, BYTE_LEN)) != -1) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return file;
}
}
調(diào)用說明
工具類中提供了兩個解壓的方式:
方式一:本地文件 File 進(jìn)行解壓。
方式二:上傳文件 MultipartFile 進(jìn)行解壓。
package com.sonar.data.controller;
import com.sonar.data.utils.business.UnZipUtils;
import com.sonar.data.vo.UnzipFileVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.List;
/**
* @author Yuanqiang.Zhang
* @since 2022/7/8
*/
@RestController
@RequestMapping("/test")
public class TestController {
/**
* 上傳文件解壓(示例)
*/
@PostMapping("/import")
public List<UnzipFileVo> importTest(@RequestPart("file") MultipartFile mFile) {
return UnZipUtils.unzip(mFile);
}
/**
* 本地文件解壓(示例)
*/
public static void main(String[] args) {
File file = new File("src.zip");
boolean delete = file.delete();
System.out.println(delete);
}
}
測試效果
我們以上傳壓縮包解析為例,通過 Postman 進(jìn)行接口測試,解析的結(jié)果如下。

以上就是Java實現(xiàn)解析zip壓縮包并獲取文件內(nèi)容的詳細(xì)內(nèi)容,更多關(guān)于Java解析zip壓縮包的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring FreeMarker整合Struts2過程詳解
這篇文章主要介紹了Spring FreeMarker整合Struts2過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
Spring中如何使用@Value注解實現(xiàn)給Bean屬性賦值
這篇文章主要介紹了Spring中如何使用@Value注解實現(xiàn)給Bean屬性賦值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
解析Mybatis的insert方法返回數(shù)字-2147482646的解決
這篇文章主要介紹了解析Mybatis的insert方法返回數(shù)字-2147482646的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
springboot集成mybatis-plus遇到的問題及解決方法
這篇文章主要介紹了springboot集成mybatis-plus遇到的問題及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
java WSDL接口webService實現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了java WSDL接口webService實現(xiàn)方式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

