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());
        }
    }
}
相关推荐
我命由我1234512 小时前
SEO 与 GEO 极简理解
java·linux·运维·开发语言·学习·算法·运维开发
金銀銅鐵12 小时前
[Java] 自己写程序,来解析方法的 descriptor
java·后端
Yang961112 小时前
0.5 米超短盲区!鼎讯信通 GO-50PRO 光时域反射仪科普
开发语言·后端·golang
红辣椒...12 小时前
codex+第三方模型
java·服务器·前端
不会C语言的男孩12 小时前
C++ Primer Plus 第12章:类和动态内存分配
开发语言·c++
一个做软件开发的牛马12 小时前
Java 继承与多态:从"是什么"到"能做什么"的设计思维
java·后端
不懂的浪漫12 小时前
05|Netty ByteBuf 源码分析:为什么不用 Java ByteBuffer
java·netty
阿里嘎多学长12 小时前
2026-05-30 GitHub 热点项目精选
开发语言·程序员·github·代码托管
wapicn9912 小时前
API接口调试笔记:从注册到第一个数据返回,全流程详解
java·开发语言·python·lua
程序员阿明12 小时前
flowable集成flowable及其运行示例spring boot后端
java·spring boot·后端