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());
        }
    }
}
相关推荐
indexsunny1 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务的逐步深入
java·数据库·spring boot·微服务·kafka·监控·安全认证
weixin_433179331 分钟前
Python - word jumble游戏
开发语言·python
小光学长7 分钟前
ssm手工艺品交易平台4xccvou1(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring
AAA.建材批发刘哥9 分钟前
03--C++ 类和对象中篇
linux·c语言·开发语言·c++·经验分享
jghhh0113 分钟前
MATLAB实现弹道仿真源代码
开发语言·matlab
拾荒的小海螺37 分钟前
C#:PdfiumViewer 高效解析和操作 PDF 的技术指南
开发语言·pdf·c#
人道领域39 分钟前
【零基础学java】(方法引用)
java·开发语言
Macbethad40 分钟前
TwinCAT半导体设备IO Simulator技术方案
开发语言
路漫聊架构1 小时前
Redis扫描大key利器Scan命令探秘
java·数据库·redis
qq_336313931 小时前
java基础-IO流(打印流)
java·开发语言