前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 实现解压 .tar.gz 这种格式的压缩包,递归文件夹,找到tar.gz 格式的压缩包,并且进行解压,解压到这个压缩包所在的文件夹下

Java 实现解压 .tar.gz 这种格式的压缩包,递归文件夹,找到tar.gz 格式的压缩包,并且进行解压,解压到这个压缩包所在的文件夹下

作者头像
一写代码就开心
发布2023-12-01 10:25:48
2190
发布2023-12-01 10:25:48
举报
文章被收录于专栏:java和pythonjava和python

1 问题

Java 实现解压 TR_2023063012.tar.gz 这种格式的压缩包

递归文件夹,找到tar.gz 格式的压缩包,并且进行解压,解压到这个压缩包所在的文件夹下

2 实现

代码语言:javascript
复制
  public static void main(String[] args) {

        String depth = "D:\\data";
        ArrayList<String> strings = new ArrayList<>();
        List<String> gz = FileUtils.listFile(strings, new File(depth), true, "gz");
        for (String  item:gz){
            File file = new File(item);
            File parentFile = file.getParentFile();
            String absolutePath = parentFile.getAbsolutePath();

                    try {
            uncompressTarGz(item, absolutePath);
            System.out.println("File uncompressed successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
            System.out.println(item);
        }


//        String inputFilePath = "D:\\059\\data\\RAIN_GRIB\\2023060200\\TR_2023060200.tar.gz"; // 压缩包文件路径
//        String outputFolderPath = "D:\\059\\data\\RAIN_GRIB\\2023060200\\"; // 解压输出文件夹路径
//
//        try {
//            uncompressTarGz(inputFilePath, outputFolderPath);
//            System.out.println("File uncompressed successfully.");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    private static void uncompressTarGz(String inputFilePath, String outputFolderPath) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(inputFilePath);
             BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
             GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(bufferedInputStream);
             TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)) {

            TarArchiveEntry entry;
            while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    File outputFile = new File(outputFolderPath, entry.getName());
                    createParentDirectory(outputFile);

                    try (OutputStream outputStream = new FileOutputStream(outputFile)) {
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = tarInputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, length);
                        }
                    }
                }
            }
        }
    }

    private static void createParentDirectory(File file) {
        File parent = file.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 问题
  • 2 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档