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
相关推荐
better_liang2 分钟前
每日Java面试场景题知识点之-Docker容器化部署悟空码字4 分钟前
SpringBoot整合Kafka,实现高可用消息队列集群天天摸鱼的java工程师4 分钟前
从等电梯到写调度系统:一个Java程序员的脑洞实践bubiyoushang88816 分钟前
基于MATLAB的非线性有限元梁扭矩分析实现qq_124987075317 分钟前
基于springboot的仁和机构的体检预约系统的设计与实现(源码+论文+部署+安装)开开心心_Every19 分钟前
免费进销存管理软件:云端本地双部署No0d1es22 分钟前
2025年12月 GESP CCF编程能力等级认证Python二级真题虫小宝26 分钟前
优惠券app安全策略:基于OAuth2.0的第三方平台授权与数据保护工程师00727 分钟前
C#中的CIL(公共中间语言)资生算法程序员_畅想家_剑魔31 分钟前
Java常见技术分享-29-Jackson JSON处理类详解