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
相关推荐
星哥说事几秒前
应用程序监控:Java 与 Web 应用的实践派大鑫wink6 分钟前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势xUxIAOrUIII29 分钟前
【Spring Boot】控制器Controller方法Dolphin_Home32 分钟前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)等....32 分钟前
Miniconda使用zfj32134 分钟前
go为什么设计成源码依赖,而不是二进制依赖醇氧34 分钟前
org.jetbrains.annotations的@Nullable 学习Java&Develop36 分钟前
Aes加密 GCM javaweixin_462446231 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)JIngJaneIL1 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)