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());
        }
    }
}
相关推荐
海奥华2几秒前
go中的接口返回设计思想
开发语言·后端·golang
lifallen2 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰2 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
k要开心3 分钟前
从C到C++语法过度1
开发语言·c++
长安不见4 分钟前
背景知识: 理解LimitLatch背后的AQS
java
小吕学编程7 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
weixin_4383354013 分钟前
Spring Boot实现接口时间戳鉴权
java·spring boot·后端
q5673152327 分钟前
IBM官网新闻爬虫代码示例
开发语言·分布式·爬虫
笨笨马甲33 分钟前
附加模块--Qt OpenGL模块功能及架构
开发语言·qt
pan_junbiao1 小时前
Spring框架的设计模式
java·spring·设计模式