JAVA-解压缩工具类TarGzUtils

targz工具类
public class TarGzUtils {

    public static void extractTarGz(String sourceFilePath, String destDir) {
        Path destPath = Paths.get(destDir);
        if (!Files.exists(destPath)) {
            FileUtil.mkdir(destDir);
        }

        try (InputStream fIn = Files.newInputStream(Paths.get(sourceFilePath));
            BufferedInputStream bIn = new BufferedInputStream(fIn);
            InputStream gzipIn = new GzipCompressorInputStream(bIn);
            TarArchiveInputStream tIn = new TarArchiveInputStream(gzipIn)) {

            TarArchiveEntry entry;
            while ((entry = tIn.getNextTarEntry()) != null) {
                Path targetPath = destPath.resolve(entry.getName());
                if (entry.isDirectory()) {
                    Files.createDirectories(targetPath);
                } else {
                    Files.copy(tIn, targetPath);
                }
            }
        } catch (Exception e) {
            throw new BusinessException("extractTarGz exception");
        }
    }

    public static void compressFolderToTarGz(String sourceDir, String tarGzPath, String srcFileName) {
        try (OutputStream fos = new FileOutputStream(tarGzPath);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            GzipCompressorOutputStream gzos = new GzipCompressorOutputStream(bos);
            TarArchiveOutputStream taos = new TarArchiveOutputStream(gzos)) {
            addFolderToTar(new File(sourceDir), srcFileName, taos);
            taos.finish();
            System.out.println("压缩成功!");

        } catch (IOException e) {
            throw new BusinessException("compressFolderToTarGz exception");
        }
    }

    private static void addFolderToTar(File folder, String base, TarArchiveOutputStream taos) throws IOException {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                String entryName = base + (base.length() == 0 ? "" : "/") + file.getName();
                TarArchiveEntry entry = new TarArchiveEntry(file, entryName);
                if (!file.isDirectory()) {
                    entry.setSize(file.length());
                }
                taos.putArchiveEntry(entry);
                if (!file.isDirectory()) {
                    try (FileInputStream fis = new FileInputStream(file)) {
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = fis.read(buffer)) > 0) {
                            taos.write(buffer, 0, length);
                        }
                    }
                }
                taos.closeArchiveEntry();
                if (file.isDirectory()) {
                    addFolderToTar(file, entryName, taos);
                }
            }
        }
    }

}

使用实例

    @Test
    void unzip(){
        String zipFilePath = "/Users/temp/targz/900000535.tar.gz";
        String unzipFilePath = "/Users/temp/targz/900000535";
        TarGzUtils.extractTarGz(zipFilePath,unzipFilePath);
        TarGzUtils.compressFolderToTarGz("/Users/temp/targz/900000535/900000535", "/Users/temp/targz/900000535/aaa.tar.gz", "900000535");
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值