一. 7z 解压缩
maven依赖
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.5</version>
</dependency>
</dependencies>
java代码
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
public class ZipUtils {
/**
* 7z文件压缩
*
* @param inputFile
* 待压缩文件夹/文件名
* @param outputFile
* 生成的压缩包名字
*/
public static void compress7z(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 + File.separatorChar);
out.putArchiveEntry(entry);
} else// 如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
{
for (int i = 0; i < flist.length; i++) {
compress(out, flist[i], name + File.separatorChar + 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 inputFile
* 待解压文件名
* @param destDirPath
* 解压路径
*/
public static void Uncompress7z(String inputFile, String destDirPath)
throws Exception {
File srcFile = new File(inputFile);// 获取当前压缩文件
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new Exception(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
SevenZFile zIn = new SevenZFile(srcFile);
SevenZArchiveEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry()) != null) {
if (!entry.isDirectory()) {
file = new File(destDirPath, entry.getName());
if (!file.exists()) {
new File(file.getParent()).mkdirs();// 创建此文件的上级目录
}
OutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
bos.close();
out.close();
}
}
}
}
————————————————
版权声明:本文为CSDN博主「就是二二二二婷」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34474324/article/details/97512377