pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/controller/ZipAndRarController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/pojo/FileMessage.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/service/ProductHistoryService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/service/ZipAndRarService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/whyc/util/ZipAndRarUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -158,6 +158,17 @@ <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!--zip和rar压缩包使用 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.12</version> </dependency> <dependency> <groupId>com.github.junrar</groupId> <artifactId>junrar</artifactId> <version>3.0.0</version> </dependency> <!--外部引入--> <dependency> <groupId>com.whyc</groupId> src/main/java/com/whyc/controller/ZipAndRarController.java
New file @@ -0,0 +1,25 @@ package com.whyc.controller; import com.whyc.dto.Response; import com.whyc.service.ZipAndRarService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @Api(tags = "压缩包") @RestController @RequestMapping("zipAndRar") public class ZipAndRarController { @Autowired private ZipAndRarService service; @ApiOperation(value = "产品查看原来压缩包中文件信息(文件目录和时间)") @GetMapping("getzipAndRarInfo") public Response getzipAndRarInfo(@RequestParam String fileUrl){ return service.getzipAndRarInfo(fileUrl); } } src/main/java/com/whyc/pojo/FileMessage.java
New file @@ -0,0 +1,10 @@ package com.whyc.pojo; import lombok.Data; @Data public class FileMessage { private String fileName; private String fileTime; private Long fileSize; } src/main/java/com/whyc/service/ProductHistoryService.java
@@ -112,7 +112,7 @@ query.eq("parent_code",parentCode).eq("custom_code",customCode).orderByDesc("version").last(" limit 1"); return mapper.selectOne(query); } //历史版本可用性设定 //历史版本可用性设定<产品中包含PCB> @Transactional public Response setpHistoryEnable(String parentCode, String customCode, int version, int enabled, String reason, String versionTime) { UpdateWrapper wrapper=new UpdateWrapper(); src/main/java/com/whyc/service/ZipAndRarService.java
New file @@ -0,0 +1,33 @@ package com.whyc.service; import com.whyc.dto.FileDirPath; import com.whyc.dto.Response; import com.whyc.pojo.FileMessage; import com.whyc.util.ZipAndRarUtil; import org.springframework.stereotype.Service; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Service public class ZipAndRarService { //产品查看原来压缩包中文件信息(文件目录和时间) public Response getzipAndRarInfo(String fileUrl) { String fileDirName = FileDirPath.getFileDirName(); List<FileMessage> list=new ArrayList<>(); try { if(fileUrl.contains("zip")){ list=ZipAndRarUtil.getZipFileList(fileDirName+ File.separator+fileUrl); }else if(fileUrl.contains("rar")){ list=ZipAndRarUtil.getRarList(fileDirName+ File.separator+fileUrl); } } catch (IOException e) { e.printStackTrace(); } return new Response().setII(1,list!=null,list,"压缩文件信息"); } } src/main/java/com/whyc/util/ZipAndRarUtil.java
New file @@ -0,0 +1,82 @@ package com.whyc.util; import com.github.junrar.Archive; import com.github.junrar.rarfile.FileHeader; import com.whyc.pojo.FileMessage; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.zip.ZipInputStream; public class ZipAndRarUtil { public static List<FileMessage> getZipFileList(String zipFileName) throws IOException { ZipInputStream zipInputStream = null; try{ FileInputStream inputStream = new FileInputStream(zipFileName); zipInputStream = new ZipInputStream( new BufferedInputStream(inputStream), StandardCharsets.UTF_8); java.util.zip.ZipEntry ze; List<FileMessage> list = new ArrayList<>(); //循环遍历 while ((ze = zipInputStream.getNextEntry()) != null) { FileMessage fileMessage=new FileMessage(); if (!ze.isDirectory()) { fileMessage.setFileName(ze.getName()); fileMessage.setFileSize(ze.getSize()); String formatted = ActionUtil.sdfwithALL.format( new Date( ze.getLastModifiedTime().toMillis() ) ); fileMessage.setFileTime(formatted); list.add(fileMessage); } } return list; } catch (Exception e) { //logger.error(" getZipOneFileContent error:" + e.getMessage()); return null; } finally { if (zipInputStream != null) { zipInputStream.closeEntry(); } } } public static List<FileMessage> getRarList(String rarFileName) throws IOException { FileInputStream inputStream = null; Archive archive = null; try { inputStream = new FileInputStream(rarFileName); archive = new Archive(inputStream); FileHeader fileHeader = archive.nextFileHeader(); List<FileMessage> list = new ArrayList<>(); while (fileHeader != null) { if (fileHeader.isDirectory()) { continue; } FileMessage fileMessage=new FileMessage(); fileMessage.setFileName(fileHeader.getFileNameString()); fileMessage.setFileSize(fileHeader.getFullUnpackSize()); String formatted = ActionUtil.sdfwithALL.format(fileHeader.getMTime()); fileMessage.setFileTime(formatted); list.add(fileMessage); fileHeader = archive.nextFileHeader(); } return list; } catch (Exception e) { //logger.error(" getRarFileList error:" + e.getMessage()); return null; } finally { if (inputStream != null) { inputStream.close(); } if (archive != null) { archive.close(); } } } }