最近项目中需要将文件和文件夹一起打包压缩为tar.gz文件,特此记录便于日后查阅。

package com.openailab.oascloud.file.util;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.*;
import java.util.zip.GZIPOutputStream;/*** @author zhangzhixiang* @version V1.0* @Title: GZIPUtil.java* @Description: 文件压缩工具类* @date 2020-01-07 19:40:50*/
public class GZIPUtil {private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);/*** tar文件批量打包(仅限于文件打包)** @param sources* @param target* @return java.io.File* @author zxzhang* @date 2020/1/7*/public static File pack(File target, File[] sources) {try (FileOutputStream out = new FileOutputStream(target); TarArchiveOutputStream os = new TarArchiveOutputStream(out)) {for (File file : sources) {os.putArchiveEntry(new TarArchiveEntry(file, file.getName()));IOUtils.copy(new FileInputStream(file), os);os.closeArchiveEntry();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return target;}/*** tar文件批量打包(支持文件&文件夹打包)** @param destFilePath* @param files* @return java.lang.String* @author zxzhang* @date 2020/3/10*/public static String tarFiles(String destFilePath, File... files) throws IOException {File destFile = new File(destFilePath);if (destFile.exists()) {LOG.error("********目标文件已存在,destFilePath:{}********" + destFilePath);return "目标文件已存在,destFilePath:" + destFilePath;}try (FileOutputStream fileOutputStream = new FileOutputStream(destFile);BufferedOutputStream bufferedWriter = new BufferedOutputStream(fileOutputStream);TarArchiveOutputStream tar = new TarArchiveOutputStream(bufferedWriter)) {tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);for (File file : files) {addTarArchiveEntryToTarArchiveOutputStream(file, tar, "");}}return null;}/*** 添加文件到tar输出流中** @param file* @param tar* @param prefix* @return void* @author zxzhang* @date 2020/3/10*/private static void addTarArchiveEntryToTarArchiveOutputStream(File file, TarArchiveOutputStream tar, String prefix) throws IOException {TarArchiveEntry entry = new TarArchiveEntry(file, prefix + File.separator + file.getName());if (file.isFile()) {entry.setSize(file.length());tar.putArchiveEntry(entry);try (FileInputStream fileInputStream = new FileInputStream(file);BufferedInputStream input = new BufferedInputStream(fileInputStream);) {IOUtils.copy(input, tar);}tar.closeArchiveEntry();} else {tar.putArchiveEntry(entry);tar.closeArchiveEntry();prefix += File.separator + file.getName();File[] files = file.listFiles();if (files != null) {for (File f : files) {addTarArchiveEntryToTarArchiveOutputStream(f, tar, prefix);}}}}/*** tar文件压缩** @param source* @return java.io.File* @author zxzhang* @date 2020/1/7*/public static File compress(String outDir, File source) {File target = new File(outDir + File.separator + source.getName() + ".gz");FileInputStream in = null;GZIPOutputStream out = null;try {in = new FileInputStream(source);out = new GZIPOutputStream(new FileOutputStream(target));byte[] array = new byte[1024];int number = -1;while ((number = in.read(array, 0, array.length)) != -1) {out.write(array, 0, number);}} catch (FileNotFoundException e) {e.printStackTrace();return null;} catch (IOException e) {e.printStackTrace();return null;} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();return null;}}if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();return null;}}}return target;}//    public static void main(String[] args) throws IOException {File[] sources = new File[]{new File("/usr/local/oas/zhangzhixiang.txt"), new File("/usr/local/oas/dockerFile")};File target = new File("/usr/local/oas/release_package.tar");pack(sources, target);compress(target);
//        String filePath = "/usr/local/oas";
//        String destFilePath = filePath + File.separator + "zhouhong" + ".tar";
//        tarFiles(destFilePath,
//                new File("/usr/local/oas/dockerFile/"),
//                new File("/usr/local/oas/笔记本文件/"),
//                new File("/usr/local/oas/UUID.info"));
//        compress("/usr/local/oas",new File(destFilePath));
//    }
}

到此 Java语言TAR文件(文件夹)批量打包&压缩介绍完成。

Java语言TAR文件(文件夹)批量打包压缩相关推荐

  1. vue中实现文件批量打包压缩下载(以及下载跨域问题分析)

    上次做了一个选择多个数据生成多个二维码并下载,当时项目催的紧,就简单写了个循环生成二维码下载,一次性会下载很多文件,特别难整理: 刚好这次项目又遇到类似这种功能,需要一次性批量下载多个文件,那么就安排 ...

  2. 计算机专业毕业设计—JAVA语言系统设计(共80套打包)

    JAVA SMART系统-系统框架设计与开发(源代码+论文).rar java Smart系统-题库及试卷管理模块的设计与开发(源代码+论文) java Smart系统-题库及试卷管理模块的设计与开发 ...

  3. java语言怎样判断文件夹_JAVA语言之如何判断文件,判断文件夹是否存在的代码...

    本文主要向大家介绍了JAVA语言之如何判断文件,判断文件夹是否存在的代码,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助. 一.判断文件是否存在,不存在则创建File file = ne ...

  4. 7z替换exe文件内容不能替换文件_Windows小技巧 批处理文件实现目录下文件批量打包压缩...

    最近整理资料,发现很多 pdf 文档,占用了不少的存储空间,考虑使用 7-zip 进行压缩存储,由于文件比较多,且分散在不同目录下,一个个文件压缩比较繁琐.为了提高效率且,方便后面取用,所有打算捣鼓个 ...

  5. c# 指定打开某个路径下的CMD_Windows小技巧 批处理文件实现目录下文件批量打包压缩...

    最近整理资料,发现很多 pdf 文档,占用了不少的存储空间,考虑使用 7-zip 进行压缩存储,由于文件比较多,且分散在不同目录下,一个个文件压缩比较繁琐.为了提高效率且,方便后面取用,所有打算捣鼓个 ...

  6. java语言实现修改文件名称的功能

    感觉最近有点自卑,不行以后写文章的时候要给自己打鸡血,现在就来一句:「生命之灯因热情而点燃,生命之舟因拼搏而前行」. 上课的时候,老师展示了他那用 matlab 语言解决了学生交实验的文件名命名不规范 ...

  7. Windows小技巧 -- 批处理文件实现目录下文件批量打包压缩

    文章目录 for 命令实现 使用方法 批处理 for 命令说明 **forfiles** 命令实现 批处理 forfiles 命令说明 关于 7-zip DEL 命令删除文件 参考阅读 最近整理资料, ...

  8. linux环境对apk文件写入数据,Linux下7zip命令apk中插入文件标识渠道批量打包

    项目需要:需要对某个包分包,对渠道和下面的包做标记,但又不改变原包的签名,想到在原apk中插入某个带渠道号和包号的文件,服务器Linux环境,采用7zip 压缩格式解压,打包,插入文件到apk解压后的 ...

  9. 如何压缩打包图片文件?照片如何打包压缩?

    平时在处理多张图片kb大小的时候,除了批量图片压缩之外,我们还可以使用图片打包压缩,将图片文件打包成文件夹来压缩处理,但是市面的压缩软件都是需要下载安装的.下面介绍一款在线打包压缩的方法,使用图片在线 ...

最新文章

  1. hookup_2.10-0.2.3.jar包下载
  2. maven的仓库:本地和远程
  3. java中try,catch,finally的作用
  4. 不小心把硬盘摔了一下,结果电脑变成这样了......
  5. 老码农的Java干货资源
  6. python 美化ppt_使用python-pptx包批量修改ppt格式的实现
  7. Andriod Studio 使用心得,持续更新中
  8. 线性代数及其应用(第三版)1.4节习题解答
  9. Flutter开发App简介
  10. 【面试常问】BS 与 CS 的联系与区别
  11. Google BBR是什么?以及在 CentOS 7 上如何部署
  12. 基于python+django框架+Mysql数据库的旅游景区景点售票系统设计与实现
  13. mongo从开始到安装以及遇到的问题
  14. 超详细的阿里云服务器购买及远程连接开机(Win系统)
  15. Facebook公司:如何删掉960万句“脏话”?
  16. mysql添加多个字段删除多个字段
  17. PHP associate with Flash or micromedia
  18. JDK9下载、安装和配置环境变量图解
  19. 培训机构是在“祸害”IT行业吗?
  20. 从面试题中学Web安全

热门文章

  1. 7个最佳餐厅应用模板
  2. NBT封面:水稻NRT1.1B基因调控根系微生物组参与氮利用(作者解读)
  3. 在项目中调外部网站接口
  4. 基于Java毕业设计毕业生交流学习平台源码+系统+mysql+lw文档+部署软件
  5. Eclipse离线下载安装Svn插件
  6. mysql 李玉婷网课配套笔记(五) 分页查询,库和表的管理
  7. HTCVIVE硬件环境搭建和软件基础配置
  8. 数据分析最常用的5大软件,你都会哪些?
  9. 学习索引: 现状与研究展望
  10. 谷歌面临欧盟110亿天价罚款!欧盟主席将赴美见特朗普