whycxzp
2024-01-03 6e4cf5045cb931cd375e5c7ee76cfe199a292890
解压
3个文件已修改
127 ■■■■■ 已修改文件
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 117 ●●●●● 补丁 | 查看 | 原始文档 | 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,83 +233,62 @@
        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);
    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()) {
                continue;
            }
            byte[] buffer = new byte[4096];
            int bytesRead;
            String entryName = entry.getName();
            //if(entryName.contains(File.separator)){
            if(entryName.contains("/")){
                String entryNameDir = entryName.substring(0, entryName.lastIndexOf("/"));
                String entryDirStr = outputFolder + File.separator + entryNameDir;
                File entryDir = new File(entryDirStr);
                if(!entryDir.exists()){
                    entryDir.mkdirs();
                }
            }
            OutputStream outputStream = new FileOutputStream(new File(outputFolder + File.separator + entryName));
            while ((bytesRead = ais.read(buffer)) > -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
        }
        return null;
    }
    private static void decompressZip(File file, String outputFolder) {
        try (ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", new FileInputStream(file))) {
            ArchiveEntry entry;
            while ((entry = ais.getNextEntry()) != null) {
                if (!ais.canReadEntryData(entry) || entry.isDirectory()) {
    private static void decompressRar(File file, String outputFolder) throws IOException, RarException {
        Archive archive = new Archive(file);
        FileHeader fileHeader = archive.nextFileHeader();
        if (fileHeader != null) {
            while (fileHeader != null) {
                if (fileHeader.isDirectory()) {
                    fileHeader = archive.nextFileHeader();
                    continue;
                }
                byte[] buffer = new byte[4096];
                int bytesRead;
                String entryName = entry.getName();
                //if(entryName.contains(File.separator)){
                if(entryName.contains("/")){
                    String entryNameDir = entryName.substring(0, entryName.lastIndexOf("/"));
                    String entryDirStr = outputFolder + File.separator + entryNameDir;
                    File entryDir = new File(entryDirStr);
                    if(!entryDir.exists()){
                        entryDir.mkdirs();
                String tempFilePath = fileHeader.getFileName();
                File out = new File(outputFolder + File.separator + tempFilePath);
                if (!out.exists()) {
                    if (!out.getParentFile().exists()) {
                        out.getParentFile().mkdirs();
                    }
                    out.createNewFile();
                }
                OutputStream outputStream = new FileOutputStream(new File(outputFolder + File.separator + entryName));
                while ((bytesRead = ais.read(buffer)) > -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                outputStream.close();
                FileOutputStream os = new FileOutputStream(out);
                archive.extractFile(fileHeader, os);
                os.close();
                fileHeader = archive.nextFileHeader();
            }
        } catch (IOException | ArchiveException e) {
            e.printStackTrace();
        }
    }
        archive.close();
    private static void decompressRar(File file, String outputFolder) {
        try {
            Archive archive = new Archive(file);
            FileHeader fileHeader = archive.nextFileHeader();
            if (fileHeader != null) {
                while (fileHeader != null) {
                    if (fileHeader.isDirectory()) {
                        fileHeader = archive.nextFileHeader();
                        continue;
                    }
                    String tempFilePath = fileHeader.getFileName();
                    File out = new File(outputFolder + File.separator + tempFilePath);
                    if (!out.exists()) {
                        if (!out.getParentFile().exists()) {
                            out.getParentFile().mkdirs();
                        }
                        out.createNewFile();
                    }
                    FileOutputStream os = new FileOutputStream(out);
                    archive.extractFile(fileHeader, os);
                    os.close();
                    fileHeader = archive.nextFileHeader();
                }
            }
            archive.close();
        } catch (IOException | RarException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {