Upload folders (multi-level files) to the server

Question: Provide the idea of uploading the folder in HTML to the designated folder of the server
: zip the level file into a zip package, upload it to the server, and decompress it to the designated folder

Project dependencies

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.18</version>
        </dependency>

Code



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

/**
 * @author Czw
 * @Description 管理员Controller
 * @Date 2019/4/17 0017 下午 3:01
 */
@Slf4j
@RestController
public class ArchiveController {
    
    

    @PostMapping("/archive")
    public String archive(MultipartFile file) {
    
    
        try {
    
    
            String path = "/opt/nginx/html/u3d/catalog1";
            unzip(file.getInputStream(), path);
            log.info("***解压缩完成***");
        } catch (IOException e) {
    
    
            log.error("***解压压缩包出错***");
            e.printStackTrace();
        }
        return "success";
    }


    /**
     * 压缩文件夹到指定输出流中,可以是本地文件输出流,也可以是web响应下载流
     *
     * @param srcDir       源文件夹
     * @param outputStream 压缩后文件的输出流
     * @throws IOException IO异常,抛出给调用者处理
     * @auther CZW
     */
    public static void zip(String srcDir, OutputStream outputStream) throws IOException {
    
    
        try (
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                ArchiveOutputStream out = new ZipArchiveOutputStream(bufferedOutputStream);
        ) {
    
    
            Path start = Paths.get(srcDir);
            Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
    
    

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
    
    
                    ArchiveEntry entry = new ZipArchiveEntry(dir.toFile(), start.relativize(dir).toString());
                    out.putArchiveEntry(entry);
                    out.closeArchiveEntry();
                    return super.preVisitDirectory(dir, attrs);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    
    
                    try (
                            InputStream input = new FileInputStream(file.toFile())
                    ) {
    
    
                        ArchiveEntry entry = new ZipArchiveEntry(file.toFile(), start.relativize(file).toString());
                        out.putArchiveEntry(entry);
                        IOUtils.copy(input, out);
                        out.closeArchiveEntry();
                    }
                    return super.visitFile(file, attrs);
                }

            });

        }
    }

    /**
     * 解压zip文件到指定文件夹
     *
     * @param zipFileName 源zip文件路径
     * @param destDir     解压后输出路径
     * @throws IOException IO异常,抛出给调用者处理
     * @auther CZW
     */
    public static void unzip(String zipFileName, String destDir) throws IOException {
    
    
        try (
                InputStream inputStream = new FileInputStream(zipFileName);
        ) {
    
    
            unzip(inputStream, destDir);
        }

    }

    /**
     * 从输入流中获取zip文件,并解压到指定文件夹
     *
     * @param inputStream zip文件输入流,可以是本地文件输入流,也可以是web请求上传流
     * @param destDir     解压后输出路径
     * @throws IOException IO异常,抛出给调用者处理
     * @auther CZW
     */
    public static void unzip(InputStream inputStream, String destDir) throws IOException {
    
    
        try (
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                ArchiveInputStream in = new ZipArchiveInputStream(bufferedInputStream);
        ) {
    
    
            ArchiveEntry entry;
            while (Objects.nonNull(entry = in.getNextEntry())) {
    
    
                if (in.canReadEntryData(entry)) {
    
    
                    File file = Paths.get(destDir, entry.getName()).toFile();
                    if (entry.isDirectory()) {
    
    
                        if (!file.exists()) {
    
    
                            file.mkdirs();
                        }
                    } else {
    
    
                        try (
                                OutputStream out = new FileOutputStream(file);
                        ) {
    
    
                            IOUtils.copy(in, out);
                        }
                    }
                } else {
    
    
                    System.out.println(entry.getName());
                }
            }
        }

    }

}

Test Results

Use postman to test the import. The results are as follows
Insert picture description here
. After the upload is complete, you can see that there are indeed uncompressed files in the server directory.
Insert picture description here


Never forget the original intention

Guess you like

Origin blog.csdn.net/qq_42910468/article/details/105278243