经过 Java 压缩文件,打包一个 tar.gz 采集器包

1、如何经过 Java 打包文件

1.1 添加 Maven 依赖html

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>

 

1.2 打包核心代码java

经过 Apachecompress 工具打包思路大体以下:linux

  • ①:建立一个 FileOutputStream 到输出文件(.tar.gz)文件。apache

  • ②:建立一个 GZIPOutputStream,用来包装 FileOutputStream对象。工具

  • ③:建立一个 TarArchiveOutputStream,用来包装 GZIPOutputStream对象。测试

  • ④:接着,读取文件夹中的全部文件。spa

  • ⑤:若是是目录,则将其添加到 TarArchiveEntrycode

  • ⑥:若是是文件,依然将其添加到 TarArchiveEntry 中,而后还需将文件内容写入 TarArchiveOutputStream 中。htm

 

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.zip.GZIPOutputStream;
public class TarUtils {    public static void compress(String sourceFolder, String tarGzPath) throws IOException {        createTarFile(sourceFolder, tarGzPath);    }    private static void createTarFile(String sourceFolder, String tarGzPath) {        TarArchiveOutputStream tarOs = null;        try {            // 建立一个 FileOutputStream 到输出文件(.tar.gz)FileOutputStream fos = new FileOutputStream(tarGzPath);            // 建立一个 GZIPOutputStream,用来包装 FileOutputStream 对象GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));            // 建立一个 TarArchiveOutputStream,用来包装 GZIPOutputStream 对象            tarOs = new TarArchiveOutputStream(gos);            // 若不设置此模式,当文件名超过 100 个字节时会抛出异常,异常大体以下:            // is too long ( > 100 bytes)            // 具体可参考官方文档:http://commons.apache.org/proper/commons-compress/tar.html#Long_File_Names            tarOs.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);            addFilesToTarGZ(sourceFolder, "", tarOs);        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                tarOs.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void addFilesToTarGZ(String filePath, String parent, TarArchiveOutputStream tarArchive) throws IOException {        File file = new File(filePath);        // Create entry name relative to parent file pat        String entryName = parent + file.getName();        // 添加 tar ArchiveEntry        tarArchive.putArchiveEntry(new TarArchiveEntry(file, entryName));        if (file.isFile()) {            FileInputStream fis = new FileInputStream(file);            BufferedInputStream bis = new BufferedInputStream(fis);            // 写入文件            IOUtils.copy(bis, tarArchive);            tarArchive.closeArchiveEntry();            bis.close();        } else if (file.isDirectory()) {            // 由于是个文件夹,无需写入内容,关闭便可            tarArchive.closeArchiveEntry();            // 读取文件夹下全部文件            for (File f : file.listFiles()) {                // 递归                addFilesToTarGZ(f.getAbsolutePath(), entryName + File.separator, tarArchive);            }        }    }    public static void main(String[] args) throws IOException {        // 测试一波,将 filebeat-7.1.0-linux-x86_64 打包成名为 filebeat-7.1.0-linux-x86_64.tar.gz 的 tar 包        compress("/Users/a123123/Work/filebeat-7.1.0-linux-x86_64", "/Users/a123123/Work/tmp_files/filebeat-7.1.0-linux-x86_64.tar.gz");    }}
相关文章
相关标签/搜索