import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.zip.GZIPInputStream;
@Slf4j
@Component
public class TarGzExtractorJie {
public static void extractTarGzFile(String tarGzFilePath, String destinationFolder) {
try {
FileInputStream fileInputStream = new FileInputStream(tarGzFilePath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
GZIPInputStream gzipInputStream = new GZIPInputStream(bufferedInputStream);
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream);
TarArchiveEntry entry;
while ((entry = tarInputStream.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
new File(destinationFolder, entry.getName()).mkdirs();
} else {
File outputFile = new File(destinationFolder, entry.getName());
File parentDirectory = outputFile.getParentFile();
if (!parentDirectory.exists()) {
parentDirectory.mkdirs();
}
OutputStream outputFileStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = tarInputStream.read(buffer)) != -1) {
outputFileStream.write(buffer, 0, length);
}
outputFileStream.close();
}
}
tarInputStream.close();
gzipInputStream.close();
bufferedInputStream.close();
fileInputStream.close();
log.info("文件解压完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String sourceFile = "D:\\tar\\6.tar.gz";
String destinationFolder = "D:\\TestDownload\\jie";
try {
extractTarGzFile(sourceFile, destinationFolder);
System.out.println("Extraction completed successfully.");
} catch (Exception e) {
System.out.println("Error occurred during extraction: " + e.getMessage());
}
}
}
tar.gz文件解压缩
gb42152872023-10-18 23:00
相关推荐
e***98579 小时前
C++跨平台开发的5大核心挑战与突破企业对冲系统官9 小时前
价格风险管理平台审批角色配置与权限矩阵设计我是一只小青蛙8889 小时前
Java分层开发:PO、BO、DTO、VO全解析步步为营DotNet9 小时前
深度剖析.NET 中CancellationToken:精准控制异步操作的关键guygg889 小时前
MATLAB利用CVX求解半定规划(SDP)波束成形矩阵的设计与实现乾元9 小时前
专栏案例合集:AI 网络工程交付的完整闭环—— 从 Demo 到 Production 的工程化方法论a努力。9 小时前
得物Java面试被问:B+树的分裂合并和范围查询优化a程序小傲9 小时前
中国电网Java面试被问:Kafka Consumer的Rebalance机制和分区分配策略lbb 小魔仙9 小时前
从零搭建 Spring Cloud 微服务项目:注册中心 + 网关 + 配置中心全流程BHXDML9 小时前
Java 常用中间件体系化解析——从单体到分布式,从“能跑”到“可控、可扩展、可演进”