whycxzp
2024-01-03 6e4cf5045cb931cd375e5c7ee76cfe199a292890
解压
3个文件已修改
45 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/ZipAndRarController.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ZipAndRarService.java 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/FileUtil.java 35 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ZipAndRarController.java
@@ -1,14 +1,18 @@
package com.whyc.controller;
import com.github.junrar.exception.RarException;
import com.whyc.dto.Response;
import com.whyc.service.ZipAndRarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.archivers.ArchiveException;
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;
import java.io.IOException;
@Api(tags = "压缩包")
@RestController
@@ -25,7 +29,7 @@
    @ApiOperation("解压")
    @GetMapping("decompress")
    public Response decompress(@RequestParam String compressedFileUrl){
    public Response decompress(@RequestParam String compressedFileUrl) throws ArchiveException, IOException, RarException {
        return service.decompress(compressedFileUrl);
    }
src/main/java/com/whyc/service/ZipAndRarService.java
@@ -1,10 +1,12 @@
package com.whyc.service;
import com.github.junrar.exception.RarException;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.pojo.FileMessage;
import com.whyc.util.FileUtil;
import com.whyc.util.ZipAndRarUtil;
import org.apache.commons.compress.archivers.ArchiveException;
import org.springframework.stereotype.Service;
import java.io.IOException;
@@ -33,7 +35,7 @@
        return new Response().setII(1,list!=null,list,"压缩文件信息");
    }
    public Response decompress(String compressedFileUrl) {
    public Response decompress(String compressedFileUrl) throws ArchiveException, IOException, RarException {
        String fileSuffix = (compressedFileUrl.substring(compressedFileUrl.lastIndexOf(".")+1));
        List<String> resList = new LinkedList<>();
        if(fileSuffix.equals("zip")||fileSuffix.equals("rar")) {
src/main/java/com/whyc/util/FileUtil.java
@@ -164,7 +164,7 @@
     * @param compressedFileUrl doc_file/xxx/xxx.zip 或者 rar
     * @return
     */
    public static List<String> decompress(String compressedFileUrl){
    public static List<String> decompress(String compressedFileUrl) throws ArchiveException, IOException, RarException {
        List<Object> resList = decompressOne(compressedFileUrl);
        File outputFolderFile = (File) resList.get(0);
        List<String>  fileList = (List<String>) resList.get(1);
@@ -197,7 +197,7 @@
     * @param compressedFileUrl doc_file/xxx/xxx.zip 或者 rar
     * @return
     */
    public static List<Object> decompressOne(String compressedFileUrl){
    public static List<Object> decompressOne(String compressedFileUrl) throws IOException, ArchiveException, RarException {
        List<Object> resList = new LinkedList<>();
        String projectDir = CommonUtil.getProjectDir() + File.separator;
        String fullFilePath;
@@ -233,23 +233,8 @@
        return resList;
    }
    public static String decompressT(File file){
        String fileName = file.getName();
        String outputFolder = "C:\\Users\\29550\\Desktop\\decompress" + File.separator + fileName;
        File outputFolderFile = new File(outputFolder);
        if(!outputFolderFile.exists()){
            outputFolderFile.mkdirs();
        }
        if(fileName.endsWith("zip")){
            decompressZip(file, outputFolder);
        }else { //rar
            decompressRar(file, outputFolder);
        }
        return null;
    }
    private static void decompressZip(File file, String outputFolder) {
        try (ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", new FileInputStream(file))) {
    private static void decompressZip(File file, String outputFolder) throws IOException, ArchiveException {
        ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", new FileInputStream(file));
            ArchiveEntry entry;
            while ((entry = ais.getNextEntry()) != null) {
                if (!ais.canReadEntryData(entry) || entry.isDirectory()) {
@@ -276,14 +261,10 @@
                outputStream.close();
            }
        } catch (IOException | ArchiveException e) {
            e.printStackTrace();
        }
    }
    private static void decompressRar(File file, String outputFolder) {
        try {
    private static void decompressRar(File file, String outputFolder) throws IOException, RarException {
            Archive archive = new Archive(file);
            FileHeader fileHeader = archive.nextFileHeader();
            if (fileHeader != null) {
@@ -307,9 +288,7 @@
                }
            }
            archive.close();
        } catch (IOException | RarException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {