Java 将文本文件压缩为 .tar.gz 并实现.tar.gz 文件的解压,需要引入如下依赖包。
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
Java 实现方法及测试方法如下所示。
package com.test.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.apache.tools.tar.TarOutputStream;
import org.springframework.util.StringUtils;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@Slf4j
public class TarGzUtil {
/**
* 压缩 .tar.gz 文件
*
* @param resourceList 源文件集合
* @param outPath 目标文件
* @throws Exception 异常
*/
public static void packet(List<File> resourceList, String outPath) throws Exception {
// 参数验证,初始化输出路径
if (resourceList == null || resourceList.size() < 1 || StringUtils.isEmpty(outPath)) {
log.error("文件压缩执行异常, 非法参数!");
}
// 迭代源文件集合,将文件打包为 tar
try (FileOutputStream fileOutputStream = new FileOutputStream(outPath + ".tmp");
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream);
TarOutputStream tarOutputStream = new TarOutputStream(bufferedOutput)) {
for (File resourceFile : resourceList) {
if (!resourceFile.isFile()) {
continue;
}
FileInputStream fileInputStream = new FileInputStream(resourceFile);
BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
TarEntry entry = new TarEntry(new File(resourceFile.getName()));
entry.setSize(resourceFile.length());
tarOutputStream.putNextEntry(entry);
IOUtils.copy(bufferedInput, tarOutputStream);
tarOutputStream.closeEntry();
}
} catch (Exception e) {
Files.delete(Paths.get(outPath + ".tmp"));
log.error("文件压缩至 {} 执行异常,嵌套异常!", outPath, e);
}
// 读取打包好的 tar 临时文件,使用 GZIP 方式压缩
try (FileInputStream fileInputStream = new FileInputStream(outPath + ".tmp");
BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream(outPath);
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(gzipOutputStream);
) {
byte[] cache = new byte[1024];
for (int index = bufferedInput.read(cache); index != -1; index = bufferedInput.read(cache)) {
bufferedOutput.write(cache, 0, index);
}
log.info("文件 {} 压缩执行完毕", outPath);
} catch (Exception e) {
log.error("文件压缩至 {} 执行异常,嵌套异常!", outPath, e);
} finally {
Files.delete(Paths.get(outPath + ".tmp"));
}
}
/**
* 解压 .tar.gz文件
*
* @param targzFile .tar.gz 压缩文件
* @param outPath 存放解压后文件的目录
*/
public static void unpack(File targzFile, String outPath) {
// 验证参数,初始化输出路径
if (targzFile == null || !targzFile.isFile() || StringUtils.isEmpty(outPath)) {
log.error("文件解压缩执行异常,非法参数!");
} else {
// 读取 .tar.gz 文件转换为 tar 文件
try (FileInputStream fileInputStream = new FileInputStream(targzFile);
BufferedInputStream bins = new BufferedInputStream(fileInputStream);
GZIPInputStream gzipInputStream = new GZIPInputStream(bins);
TarInputStream tarIn = new TarInputStream(gzipInputStream, 1024 * 2)) {
// 迭代 tar 文件集合,解压文件
for (TarEntry entry = tarIn.getNextEntry(); entry != null; entry = tarIn.getNextEntry()) {
File targetFileName = new File(outPath + "/" + entry.getName());
IOUtils.copy(tarIn, new FileOutputStream(targetFileName));
}
log.info("文件 {} 解压执行完毕", targzFile);
} catch (Exception e) {
log.error("{} 解压执行异常!", targzFile, e);
}
}
}
public static void main(String[] args) {
File file1 = new File("D:\\test\\testfile1");
File file2 = new File("D:\\test\\testfile2");
List<File> resourceList = new ArrayList<>();
resourceList.add(file1);
resourceList.add(file2);
String outPath = "D:\\test\\testfile.tar.gz";
try {
packet(resourceList, outPath);
} catch (Exception e) {
log.error("打包压缩文件出现异常", e);
}
unpack(new File(outPath), "D:\\test\\test2");
}
}