网站首页 > 技术文章 正文
文件的压缩以及解压缩技术是现代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[1024];
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[1024];
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[1024];
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[1024];
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接口中来讲多个文件进行压缩之后再下载的操作,通过这个操作开发者可以快速的实现文件的批量下载操作。有兴趣的读者可以基于上面的代码继续对文件操作进行扩展,遇到问题可以在评论区留言讨论。
猜你喜欢
- 2024-12-02 Squoosh - 谷歌出品的免费开源图片压缩工具,压缩90%,支持 API 调用
- 2024-12-02 实用技巧分享:你知道如何实现油猴脚本的导出与本地导入吗?
- 2024-12-02 前端入门——html 中如何使用图片
- 2024-12-02 超好用 Vue.js 图片裁切组件Vue-ImgCutter
- 2024-12-02 Vue项目优化篇
- 2024-12-02 热门在线图片压缩工具,一次解决平时在办公时遇到的压缩问题
- 2024-12-02 Java pdf下载优化:Java图片压缩
- 2024-12-02 在线图片压缩效果怎么样?有没有免费的工具?
- 2024-12-02 手搓一个TinyPng压缩图片的WebpackPlugin
- 2024-12-02 前端开发使用工具 gulp
你 发表评论:
欢迎- 07-08记oracle日志挖掘实操&查询归档不正常增长情况(一)
- 07-08Oracle 伪列!这些隐藏用法你都知道吗?
- 07-08orcl数据库查询重复数据及删除重复数据方法
- 07-08重大故障!业务核心表被truncate删除,准备跑路……
- 07-08oracle数据恢复—oracle执行truncate命令误删除数据的数据恢复
- 07-08Oracle-rac 修改scanip(oracle 修改sequence cache)
- 07-08ORACLE RAC CDB和PDB切换(oracle数据库rac切换)
- 07-08Oracle rac haip作用(oracle rac的典型特征)
- 596℃几个Oracle空值处理函数 oracle处理null值的函数
- 590℃Oracle分析函数之Lag和Lead()使用
- 577℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 573℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 569℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 562℃【数据统计分析】详解Oracle分组函数之CUBE
- 549℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 542℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
-
- 记oracle日志挖掘实操&查询归档不正常增长情况(一)
- Oracle 伪列!这些隐藏用法你都知道吗?
- orcl数据库查询重复数据及删除重复数据方法
- 重大故障!业务核心表被truncate删除,准备跑路……
- oracle数据恢复—oracle执行truncate命令误删除数据的数据恢复
- Oracle-rac 修改scanip(oracle 修改sequence cache)
- ORACLE RAC CDB和PDB切换(oracle数据库rac切换)
- Oracle rac haip作用(oracle rac的典型特征)
- 新手小白怎么学UI设计 推荐学习路线是什么
- 超实用!0基础UI设计自学指南(0基础学ui设计好就业吗)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端懒加载 (49)
- 前端获取当前时间 (50)
- 前端接口 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle的函数 (57)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)