tar.gz文件解压缩

复制代码
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());
        }
    }
}
相关推荐
AIGCExplore6 分钟前
Jenkins 全局配置及工具验证教程
java·servlet·jenkins
qq_3181215911 分钟前
Java大厂面试故事:Spring Boot、微服务与AI场景深度解析
java·spring boot·redis·微服务·ai·kafka·spring security
superman超哥13 分钟前
Rust 异步时间管理核心:Tokio 定时器实现机制深度剖析
开发语言·rust·编程语言·rust异步时间管理核心·tokio定时器实现机制·tokio定时器
朔北之忘 Clancy14 分钟前
2025 年 9 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·数学·青少年编程·题解
玛丽莲茼蒿17 分钟前
javaSE 集合框架(五)——java 8新品Stream类
java·开发语言
wjs202421 分钟前
SQLite Glob 子句详解
开发语言
程序员小假24 分钟前
设计一个支持万人同时抢购商品的秒杀系统?
java·后端
youyicc28 分钟前
Qt连接Pg数据库
开发语言·数据库·qt
量子炒饭大师30 分钟前
【C++入门】Cyber底码作用域的隔离协议——【C++命名空间】(using namespace std的原理)
开发语言·c++·dubbo
L***d67031 分钟前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端