zoukankan      html  css  js  c++  java
  • Java将文本文件压缩为tar.gz

        /**
         * @功能描述 压缩tar.gz 文件
         * @param sources 源文件集合
         * @param outPath 目标文件名称 无后缀的 例子 G:ackuplogstash-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();
                }
            }
        }
  • 相关阅读:
    记一次dba面试
    MySQL登陆 socket 问题
    推荐一些MySQL的博文(持续更新)
    MySQL 参数调优工具--tuning-primer
    当扫描的数据超过了全表的17%就不使用索引
    MySQL 5.7 新增参数
    MySQL 5.7 和 MySQL 5.6参数默认值比较
    MySQL创建的用户无法从本地登陆
    含有IN的子查询
    索引大小对语句执行速度的影响
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/12762250.html
Copyright © 2011-2022 走看看