Java SpringBoot 7z 压缩、解压

cmd 7z 文件压缩

7z压缩测试

添加依赖

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.5</version>
</dependency>

ZipFileUtil

package com.vipsoft.web.utils;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile; import java.io.*;
import java.util.HashMap;
import java.util.Map; public class ZipFileUtil {
/**
* 7z文件压缩
*
* @param inputFile 待压缩文件夹/文件名
* @param outputFile 生成的压缩包名字
*/ public static void zip7z(String inputFile, String outputFile) throws Exception {
File input = new File(inputFile);
if (!input.exists()) {
throw new Exception(input.getPath() + "待压缩文件不存在");
}
SevenZOutputFile out = new SevenZOutputFile(new File(outputFile));
compress(out, input, null);
out.close();
} /**
* @param name 压缩文件名,可以写为null保持默认
*/
//递归压缩
public static void compress(SevenZOutputFile out, File input, String name) throws IOException {
if (name == null) {
name = input.getName();
}
SevenZArchiveEntry entry = null;
//如果路径为目录(文件夹)
if (input.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = input.listFiles(); if (flist.length == 0)//如果文件夹为空,则只需在目的地.7z文件中写入一个目录进入
{
entry = out.createArchiveEntry(input,name + "/");
out.putArchiveEntry(entry);
} else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
{
for (int i = 0; i < flist.length; i++) {
compress(out, flist[i], name + "/" + flist[i].getName());
}
}
} else//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入7z文件中
{
FileInputStream fos = new FileInputStream(input);
BufferedInputStream bis = new BufferedInputStream(fos);
entry = out.createArchiveEntry(input, name);
out.putArchiveEntry(entry);
int len = -1;
//将源文件写入到7z文件中
byte[] buf = new byte[1024];
while ((len = bis.read(buf)) != -1) {
out.write(buf, 0, len);
}
bis.close();
fos.close();
out.closeArchiveEntry();
}
} /**
* 7z解压缩
* @param z7zFilePath 7z文件的全路径
* @return 压缩包中所有的文件
*/
public static Map<String, String> unZip7z(String z7zFilePath){ String un7zFilePath = ""; //压缩之后的绝对路径 SevenZFile zIn = null;
try {
File file = new File(z7zFilePath);
un7zFilePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".7z"));
zIn = new SevenZFile(file);
SevenZArchiveEntry entry = null;
File newFile = null;
while ((entry = zIn.getNextEntry()) != null){
//不是文件夹就进行解压
if(!entry.isDirectory()){
newFile = new File(un7zFilePath, entry.getName());
if(!newFile.exists()){
new File(newFile.getParent()).mkdirs(); //创建此文件的上层目录
}
OutputStream out = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[(int)entry.getSize()];
while ((len = zIn.read(buf)) != -1){
bos.write(buf, 0, len);
}
bos.flush();
bos.close();
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (zIn != null)
zIn.close();
} catch (IOException e) {
e.printStackTrace();
}
} Map<String, String> resultMap = getFileNameList(un7zFilePath, "");
return resultMap;
} /**
* 获取压缩包中的全部文件
* @param path 文件夹路径
* @childName 每一个文件的每一层的路径==D==区分层数
* @return 压缩包中所有的文件
*/
private static Map<String, String> getFileNameList(String path, String childName) {
System.out.println("path:" + path + "---childName:"+childName);
Map<String, String> files = new HashMap<>();
File file = new File(path); // 需要获取的文件的路径
String[] fileNameLists = file.list(); // 存储文件名的String数组
File[] filePathLists = file.listFiles(); // 存储文件路径的String数组
for (int i = 0; i < filePathLists.length; i++) {
if (filePathLists[i].isFile()) {
files.put(fileNameLists[i] + "==D==" + childName, path + File.separator + filePathLists[i].getName());
} else {
files.putAll(getFileNameList(path + File.separator + filePathLists[i].getName(), childName + "&" + filePathLists[i].getName()));
}
}
return files;
}
}

Test

package com.vipsoft.web;

import com.vipsoft.web.utils.ZipFileUtil;
import org.junit.jupiter.api.Test; public class ZipTest { @Test
void zip7zTest() throws Exception {
String inputPath = "D:\\temp\\logs\\logs";
String outputPath = "D:\\temp\\logs\\1.7z";
ZipFileUtil.zip7z(inputPath, outputPath);
} @Test
void unZip7zTest() throws Exception {
String outputPath = "D:\\temp\\logs\\1.7z";
ZipFileUtil.unZip7z(outputPath);
}
}

Java SpringBoot 7z 压缩、解压的更多相关文章

  1. java 字符串zlib压缩/解压

    今天在测公司的中间件时发现,增加netty自带的zlib codec压缩处理后,就报decompress failed, invalid head之类的异常.后来发现,直接用bytebuf处理报文体是 ...

  2. 7z 压缩解压简单示例

    7z命令行压缩示例: 7z -tZip a test.zip ./test/* -mx0 把test文件夹中所有文件以存储压缩的模式压缩成zip格式的文件,压缩文件为test.zip a为添加选项   ...

  3. 使用SevenZipSharp压缩/解压7z格式

    7z格式采用的LZMA算法,号称具有现今最高压缩率.笔者在nuget上搜索7z,在搜索结果中最终选择了SevenZipSharp来进行压缩/解压.不得不说,SevenZipSharp的API设计得非常 ...

  4. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  5. C#中使用7Z进行压缩解压

    SevenZipSharp相关文档下载地址: http://sevenzipsharp.codeplex.com/releases/view/51254 1. 解决方案中添加引用:SevenZipSh ...

  6. linux中tar及压缩解压命令用法

    把常用的tar解压命令总结下,当作备忘: tar 命令可以为Linux的文件和目录创建档案.利用 tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向 档案中加入新的文件.t ...

  7. 使用C#压缩解压rar和zip格式文件

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  8. tar 压缩解压命令详解

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  9. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

  10. 解决Linux与Windows压缩解压中文文件名乱码(转载)

    转自:http://crazyfeng.com/linux-windows-compress-chinese-filename.html 由于Linux与Windows编码问题,使用Zip Tar 压 ...

随机推荐

  1. crontab命令加载和使用

    crontab命令用于设置周期性被执行的指令. 在Linux系统中,Linux任务调度的工作主要分为以下两类:1.系统执行的工作:系统周期性所要执行的工作,如备份系统数据.清理缓存2.个人执行的工作: ...

  2. 初学,Markdown的使用

    Markdown学习 一级标题:"#"+空格+"标题" 二级标题 二级标题:"##"+空格+"标题" 三级标题 三级标题 ...

  3. C Ⅷ

    数组  int number[100];   //这个数组可以放100个数 int x; int cnt = 0; double sum = 0; scanf("%d", & ...

  4. 实验3 C语言分支语句和循环语句编程应用

    任务一 #include<math.h> #include<stdio.h> int main() { float a,b,c,x1,x2; float delta,real, ...

  5. 9. 实现包括前端后台的预约洗狗功能 - 使用Power Automate发送预约邮件 - 使用Power Automate发送带选择按钮(option)的邮件

    ​ 除了发送普通的电子邮件外,我们还可以选择发送带选项的电子邮件来得到客户的反馈,下面我们就一起来创建带有选择功能的电子邮件吧. 1. 打开我们的Power Portal,在左侧导航栏选择流,点击左上 ...

  6. html、css、js 压缩或混淆方法

    普通的压缩代码的方法包括在线工具和服务器打包处理,有一个共同的痛点是:压缩后的代码无法还原成原始的带有注释的源代码.正如大家所知,在源代码中调试Bug事半功倍.在线工具HCJCompress(ihon ...

  7. centOS7 + MongoDB 3.6.22 集群搭建 - 切片+副本集 - 个人学习

    因为我是学习这个,所以是安装成功之后自己再记录一下过程,mongodb是重新安装的,参考博客:MongoDB 3.6.9 集群搭建 - 切片+副本集 1. 服务结构介绍 结构图: 结构图解: 1. S ...

  8. ElementPlus 表单 resetFields 无效问题解决方法

    最近在写一个项目,一个表单递交或者使用resetFields关闭后,再打开,原来的值还存在,后查了一下网上的方法,确定是el-form-item,必须要加prop,其值要与model相同,此问题得到完 ...

  9. 使用web client对 vcenter 进行补丁升级

    使用web client对 vcenter 进行补丁升级 背景:最近VMware官网发布了最新的VMware vCenter Server 7.0 iso补丁文件,为了安全起故此对vCenter 进行 ...

  10. 网络如何运作——详细DNS、HTTP、网站

    详细的DNS 什么是DNS? DNS(域名系统)为我们提供了一种简单的方式来与互联网上的设备进行通信,而无需记住复杂的数字.就像每个房子都有一个唯一的地址可以直接向它发送邮件一样,互联网上的每台计算机 ...