PHP zip_entry_compressedsize() 函数_W3School PHP 参考手册

详细介绍一下Spring Boot中对于文件的压缩以及解压缩操作?

文件的压缩以及解压缩技术是现代Web应用开发过程中的一个常见的需求技术,例如对于用户上传的文件进行压缩、然后生成压缩文件进行数据下载操作。而作为后端热门技术Spring Boot,如何在Spring Boot中实现相关的操作就成了我们要讨论的关键,下面我们就来看看如何在Spring Boot中实现对于文件的压缩以及解压缩的操作吧。

常用的压缩格式

在Java中,一般情况下,我们可以通过java.util.zip和java.util.jar来实现文件压缩处理,这两个类库主要支持的文件操作格式如下所示。

  • ZIP格式:常见的压缩文件格式,支持多个文件的压缩。
  • GZIP格式:通常用于单一文件的压缩,配合Tar使用以形成.tar.gz文件。

这两种格式也是在我们日常开发中比较常见的两种压缩文件处理格式。那么基于两种格式的文件的压缩,下面我们就来看看详细的实现步骤吧。

ZIP文件的压缩

所谓的ZIP压缩其实是指将一个或者多个文件压缩成一个.zip后缀的文件。这样可以方便前端进行数据的批量下载,如下所示,实现了多个文件的压缩操作。

import java.io.*;
import java.nio.file.*;
import java.util.zip.*;

public class ZipUtil {

    public static void compressToZip(String outputZipFile, String... filesToCompress) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(outputZipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {
             
            for (String filePath : filesToCompress) {
                File file = new File(filePath);
                try (FileInputStream fis = new FileInputStream(file)) {
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zos.putNextEntry(zipEntry);

                    byte[] buffer = new byte[];
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                    }

                    zos.closeEntry();
                }
            }
        }
    }
}

在主方法中,我们可以通过调用该工具类进行文件的压缩操作,如下所示。

public static void main(String[] args) {
    try {
        ZipUtil.compressToZip(&#;output.zip&#;, &#;file1.txt&#;, &#;file2.txt&#;);
        System.out.println(&#;压缩成功!&#;);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

ZIP文件的解压缩

所谓的文件解压缩其实就是指将ZIP文件解压成分开的各个文件,我们可以通过如下的操作来实现文件的加压缩操作。

import java.io.*;
import java.util.zip.*;

public class ZipUtil {

    public static void decompressZip(String zipFilePath, String destDir) throws IOException {
        File dir = new File(destDir);
        if (!dir.exists()) dir.mkdirs();

        try (FileInputStream fis = new FileInputStream(zipFilePath);
             ZipInputStream zis = new ZipInputStream(fis)) {
             
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                File newFile = new File(destDir, zipEntry.getName());
                System.out.println(&#;解压文件: &#; + newFile.getAbsolutePath());

                if (zipEntry.isDirectory()) {
                    newFile.mkdirs();
                } else {
                    new File(newFile.getParent()).mkdirs();
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        byte[] buffer = new byte[];
                        int length;
                        while ((length = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }
                    }
                }
                zis.closeEntry();
            }
        }
    }
}

然后在主方法中我们可以调用工具类的解压缩方法进行ZIP文件的解压缩操作。

public static void main(String[] args) {
    try {
        ZipUtil.decompressZip(&#;output.zip&#;, &#;outputDir&#;);
        System.out.println(&#;解压成功!&#;);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

GZIP文件的压缩

GZIP文件其实是在Linux系统中的一种常见的一种文件压缩格式,主要是将单一文件已进行的压缩操作,所以实现起来比较简单,如下所示。

import java.io.*;
import java.util.zip.GZIPOutputStream;

public class GzipUtil {

    public static void compressToGzip(String sourceFile, String gzipFile) throws IOException {
        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileOutputStream fos = new FileOutputStream(gzipFile);
             GZIPOutputStream gzipOS = new GZIPOutputStream(fos)) {
             
            byte[] buffer = new byte[];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                gzipOS.write(buffer, 0, length);
            }
        }
    }
}

在主方法中,我们可以调用这个方法,将其进行压缩来减小大文件的体积。

public static void main(String[] args) {
    try {
        GzipUtil.compressToGzip(&#;file.txt&#;, &#;file.txt.gz&#;);
        System.out.println(&#;GZIP压缩成功!&#;);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

GZIP文件的解压缩

与文件压缩相反的操作。如下所示

import java.io.*;
import java.util.zip.GZIPInputStream;

public class GzipUtil {

    public static void decompressGzip(String gzipFile, String outputFile) throws IOException {
        try (FileInputStream fis = new FileInputStream(gzipFile);
             GZIPInputStream gzipIS = new GZIPInputStream(fis);
             FileOutputStream fos = new FileOutputStream(outputFile)) {
             
            byte[] buffer = new byte[];
            int length;
            while ((length = gzipIS.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
        }
    }
}

在主方法中通过调用工具类的解压缩方法,我们就可以实现文件的加压缩操作。

public static void main(String[] args) {
    try {
        GzipUtil.decompressGzip(&#;file.txt.gz&#;, &#;file.txt&#;);
        System.out.println(&#;GZIP解压成功!&#;);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

上面我们演示了如何单独的实现文件的压缩以及解压缩操作,下面我们就来看看上述操作如何与Spring Boot应用操作进行整合。

集成到Spring Boot应用中

其实这种文件操作主要的作用就是在文件的上传下载操作中,下面我们就来实现一个简单的文件处理的控制器,用来实现下载压缩操作。

@RestController
@RequestMapping(&#;/api/files&#;)
public class FileController {

    @PostMapping(&#;/compress&#;)
    public ResponseEntity<String> compressFiles(@RequestParam(&#;files&#;) MultipartFile[] files) {
        try {
            String zipFileName = &#;compressed.zip&#;;
            Path tempDir = Files.createTempDirectory(&#;temp-files&#;);
            
            for (MultipartFile file : files) {
                Path tempFile = tempDir.resolve(file.getOriginalFilename());
                file.transferTo(tempFile.toFile());
            }
            
            String zipFilePath = tempDir.resolve(zipFileName).toString();
            ZipUtil.compressToZip(zipFilePath, tempDir.toFile().listFiles(file -> !file.getName().equals(zipFileName)));
            
            return new ResponseEntity<>(&#;压缩成功: &#; + zipFilePath, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

当然,这里我们需要注意,通过文件压缩操作可以将多个文件进行批量的下载,当时也同时需要注意在文件下载过程中需要防止出现目录穿透的漏洞,当然对于一些大文件来讲,我们需要对其进行分块处理或者是通过流式处理的方式进行下载。

总结

上面的代码中,我们分别介绍了ZIP文件和GZIP文件的压缩以及解压缩操作,并且演示了如何在Spring Boot接口中来讲多个文件进行压缩之后再下载的操作,通过这个操作开发者可以快速的实现文件的批量下载操作。有兴趣的读者可以基于上面的代码继续对文件操作进行扩展,遇到问题可以在评论区留言讨论。

原文链接:,转发请注明来源!