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());
        }
    }
}
相关推荐
想学后端的前端工程师1 分钟前
【Java JVM虚拟机深度解析:从原理到调优】
java·jvm·python
Ricardo_03241 分钟前
关于死锁问题的学习总结
android·java
az3132 分钟前
Spring Bean初始化机制详解
java·spring·bean·初始化
夜泉_ly8 分钟前
期末速通 -Java程序设计基础 -理论
java·开发语言
这是程序猿12 分钟前
基于java的SpringBoot框架汽车销售系统
java·spring boot·spring·汽车·汽车销售网站
SunnyDays101113 分钟前
Java 高效 TXT 转 Word 指南:单文件、批量及格式美化操作
java·txt转word·文本文件转word·文本转word
m0_6113493114 分钟前
什么是副作用(Side Effects)
开发语言·前端·javascript
不急不躁12315 分钟前
Android16 跳过GMS测试项
android·java
oioihoii18 分钟前
C++多线程中join与detach机制深度解析
java·jvm·c++
雨中飘荡的记忆19 分钟前
深入理解 Guava EventBus:让你的系统解耦更优雅
java·后端