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