/**
     * @功能描述 压缩tar.gz 文件
     * @param sources 源文件集合
     * @param outPath 目标文件名称 无后缀的 例子 G:\backup\logstash-2020.04.22
     * @return 返回压缩结果
     * @throws Exception
     */
    public static void packet(String[] sources, String outPath) throws Exception {
        // gz文件 名称  TAR GZ 就是 .tar.gz
        String gzPath = String.format("%s%s%s",outPath, TAR, GZ);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        TarArchiveOutputStream tarArchiveOutputStream = null;
        GZIPOutputStream gzipOutputStream = null;
        try {
            tarArchiveOutputStream = new TarArchiveOutputStream(byteArrayOutputStream);
            // 将所有文件打包成 tar文件
            try {
                for (String source : sources) {
                    File file = new File(source);
                    tarArchiveOutputStream.putArchiveEntry(new TarArchiveEntry(file, file.getName()));
                    IOUtils.copy(new FileInputStream(file), tarArchiveOutputStream);
                    tarArchiveOutputStream.closeArchiveEntry();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(tarArchiveOutputStream != null) {
                    tarArchiveOutputStream.flush();
                    tarArchiveOutputStream.close();
                }
            }
            gzipOutputStream = new GZIPOutputStream(new FileOutputStream(gzPath));
            gzipOutputStream.write(byteArrayOutputStream.toByteArray());
        } finally {
            if(byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
            if(gzipOutputStream != null) {
                gzipOutputStream.flush();
                gzipOutputStream.close();
            }
        }
    }

 

相关文章:

  • 2022-03-10
  • 2022-12-23
  • 2021-06-04
  • 2021-04-14
  • 2022-12-23
  • 2021-12-31
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2022-02-07
  • 2022-12-23
  • 2021-12-13
  • 2021-10-15
  • 2022-12-23
  • 2022-02-06
  • 2021-12-14
相关资源
相似解决方案