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
相关推荐
sayang_shao几秒前
C++智能指针【笔记】人道领域3 分钟前
【零基础学java】(Stream流)喜欢猪猪9 分钟前
深度解析 SGLang:大模型编程新范式——从 Prompt Engineering 到 Structured Generation 的系统性跃迁两个蝴蝶飞12 分钟前
Java量化系列(九):实现股票列表自动同步,精准监控新增、更名与退市动态独自破碎E23 分钟前
Java对象是怎么在虚拟机中存储的?坚持学习前端日记27 分钟前
Android JS桥技术深度解析兮动人38 分钟前
打破 OS 壁垒:Java 跨平台硬件信息采集的“终极方案”一路往蓝-Anbo1 小时前
STM32单线串口通讯实战(一):物理层拓扑与STM32G0硬件配置weixin_307779131 小时前
MATLAB动态演示流体扩散仿真模拟的简单例子json{shen:"jing"}1 小时前
07_表单输入绑定